【问题标题】:How to create cluster column chart in R? [duplicate]如何在 R 中创建聚类柱形图? [复制]
【发布时间】:2018-08-25 09:45:41
【问题描述】:

我需要绘制 2 个类别在 4 个组中的比率,以便进行简单易懂的比较。

到目前为止,我这样做并得到了 2 个地块:

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")

我想比较 2 个类别中 4 个组的单词的比率。因此,第 1 类的第一比率 (0.50) 与“Mütter über Söhne”组的第 2 类的第一比率 (0.51) 相比。我还需要为 x 轴 =“组”和 y 轴 =“单词比率”命名。 它应该看起来像这样:

有人可以帮我吗?

【问题讨论】:

  • 非常感谢!!!这看起来很棒!如果有一种方法可以擦除栅栏后面的背景中的网格,那将是完美的。否则我完全可以在我的硕士论文中使用它。
  • @Maurits Evers 但我认为,我的回答也很好:(

标签: r plot bar-chart


【解决方案1】:

如果你不熟悉ggplot2,可以使用这种基本方式。

x1 <- c(0.50, 0.53, 0.49, 0.47)
x2 <- c(0.51, 0.54, 0.46, 0.49)
group <- c("group1", "group2", "group3", "group4")

plot.new()
plot.window(xlim = c(1, 20), ylim = c(0, 0.6))
abline(h = 0:6/10, col = "grey")
barplot(rbind(x1, x2), beside = T, space = c(0.5, 2), axes = F,
        col = c("grey10", "grey"), xlab = "groups", ylab = "ratios of words",
        names.arg = group, add = T)
legend(x = "topright", legend = c("category 1", "category 2"),
       fill = c("grey10", "grey"), box.col = NA)
mtext(0:6/10, side = 2, at = 0:6/10)

space 由两个数字指定,其中第一个是同一组中的条之间的间距,第二个是组之间的间距。

【讨论】:

  • 这与 PO 的要求无关。您首先需要以正确的方式定义数据集,而您的 rbind 不会这样做
  • 哇,谢谢!看起来很棒!有没有办法命名条形的不同颜色(黑色 = 类别 1,灰色 = 类别 2)以及 x 轴和 y 轴?
  • 这仍然不是正确的数据集。您尚未在数据集中考虑 group
  • 也许我的回答是沉重而原始的,但我不认为这是错误的。提供的数据是矢量的,所以我使用矢量的方式进行可视化。在 ggplot2 中定义数据集和组是必要的,而不是在任何地方。
  • @LauraAtaraxia 我更新了我的答案。
【解决方案2】:

这是你的正确答案。您需要首先以正确的方式定义您的数据集:

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画出来

    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())

最后的输出是:

【讨论】:

  • @Laura Ataraxia 如你所愿,我已经更新了我的答案
  • 现在我收到一条错误消息:“.Call.graphics(C_palette2, .Call(C_palette2, NULL)) 中的错误:无效的图形状态”
  • @LauraAtaraxia 确保您已在 RStudio 中安装了 ggplot2 lpackage。您可以观看此视频以了解如何安装软件包youtube.com/watch?v=rHPcUthPk_c
  • 我知道如何安装软件包,谢谢 :) 一切都好!谢谢你的回答,帮了大忙!
  • @LauraAtaraxia 你能接受我的回答吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
  • 2010-11-28
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多