使用ggplot绘制分组数据

时间:2020-08-05 14:03:15

标签: r ggplot2 dplyr tidyverse

嗨,我有一张这样的桌子:

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轴上,并且通过条形图,基于“值”列可以看到比较结果。

如何做到这一点? 问候

1 个答案:

答案 0 :(得分:2)

您要刻面吗?例如。像这样的东西:

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-08-05创建

相关问题