如何在R中创建群集柱形图? [英] How to create cluster column chart in R?
问题描述
我需要在4个组中绘制2个类别的比率,以便进行易于理解的比较.
I need to plot the ratios of 2 categories over 4 groups in order of a nice easy understandable comparison.
到目前为止,我这样做了,得到了2个地块:
i did this so far and got 2 plots:
x <- c(0.50, 0.53, 0.49, 0.47)
names(x) <- c("Mütter über Söhne", "Mütter über Töchter", "Väter über Söhne",
"Väter über Töchter")
barplot(x, xlab= "Gruppe", ylab = "Anteil Wörter abstrakt-logisch ", main =
"abstrakt-logisch")
x1 <- c(0.51, 0.54, 0.46, 0.49)
names(x1) <- c("Mütter über Söhne", "Mütter über Töchter", "Väter über Söhne", "Väter über Töchter")
barplot(x1, xlab= "Gruppe", ylab = "Anteil Wörter kreativ-verbal ", main = "kreativ-verbal")
我想比较4组中2个类别中单词的比率.因此,在MütterüberSöhne"组中,类别1的第一比率(0.50)与类别2的第一比率(0.51)相比较.我还需要为x轴=组"和y轴=单词的比率"命名.它应该看起来像这样:
I want to compare the ratios of words in 2 categories over 4 groups. so first ratio from category 1 (0.50) compared to first ratio of category 2 (0.51) over the group "Mütter über Söhne". i also need a naming for x-axis = "groups" and y-axis = "ratios of words". it should look something like that:
有人可以帮我吗?
推荐答案
这是您的正确答案.您需要首先以适当的方式定义数据集:
Here is the correct answer of yours. You need to first define your dataset in a proper way:
dt<-data.frame(
group= c("Mütter über Söhne", "Mütter über Töchter", "Väter über Söhne", "Väter über Töchter"),
category=c("category1","category1","category1","category1", "category2","category2","category2","category2"),
ratio= c(0.50, 0.53, 0.49, 0.47, 0.51, 0.54, 0.46, 0.49))
head(dt)
然后使用ggplot绘制
And then use the ggplot to draw it
library(ggplot2)
ggplot(data=dt, aes(x=group, y=ratio, fill=category)) +
geom_bar(stat="identity", position=position_dodge())+
scale_fill_brewer(palette="Paired")+ #Remove background
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
最后的输出是:
这篇关于如何在R中创建群集柱形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!