【问题标题】:Plot grouped data using ggplot使用 ggplot 绘制分组数据
【发布时间】:2020-11-25 17:32:57
【问题描述】:

您好,我有一张这样的桌子:

Question Age_Group Value
Question 1 10-20 15
Question 2 10-20 16
Question 3 10-20 12
.....
Question 1 20-30 13
Question 2 20-30 15
Question 3 20-30 18

我想建立一个直方图,其中问题在 y 轴上,年龄组在 x 轴上,并且通过柱状图可以看到基于 Value 列的比较。

如何做到这一点? 问候

【问题讨论】:

  • 你能用你的代码提供一个可重现的例子吗?检查此线程和dput 选项stackoverflow.com/questions/5963269/…
  • 嗨@Arault。实际上我无法为这个情节提供 ggplot 代码,因为这是我正在考虑的
  • 你能添加数据集的 dput 输出吗?因此我们可以尝试使用您的数据,而无需手动复制数据框

标签: r ggplot2 dplyr tidyverse


【解决方案1】:

你想要分面吗?例如。像这样:

set.seed(123)
DF <- data.frame(Question = rep(paste0("Question ", 1:10), 80),
  Age_Group = factor(sample(c("10-20", "20-30", "30-40", "40-50"), 
    800, replace=TRUE)),
  Value = sample(8:30, 800, replace = TRUE))
DF$Question <- factor(DF$Question, unique(DF$Question))

library(ggplot2)  
ggplot(DF, aes(x=Value)) + 
  geom_histogram() +
  facet_grid(Question ~ Age_Group) +
  theme(strip.text.y = element_text(angle = 0))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

reprex package (v0.3.0) 于 2020 年 8 月 5 日创建

【讨论】:

    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 2012-10-17
    • 2018-07-02
    • 2017-01-19
    • 2016-04-17
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多