在一幅图中简化多个箱形图

问题描述

我有一个包含17个问题(Q1-Q17)和分类变量(区域)的数据集。

> df[,c("Region",QUESTIONS)]
# A tibble: 963 x 18
   Region     Q1    Q2    Q3    Q4    Q5    Q6    Q7    Q8    Q9   Q10   Q11   Q12   Q13   Q14   Q15
   <chr>   <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
 1 USA         0     1     0     0     0     0     0     0     0     0     0     0     0     0     0
 2 USA         8     8     8     8     6     8     8     0     5    10     7     0     0    10     8
 3 USA         9     8     7    10     8     4     8     0     5     8     8     8     2     7     6
 4 USA         4     2     5     4     3     3     2     0     1     0     0     0     3     2     0
 5 USA         2     6     7     5     6     2     9     0     6     7     3     0     0     8     5
 6 USA         6     6     8     1     2     0     4     0     0     4     0     6    10     0     1
 7 USA         5     2     7     8    10     9    10     8     6    10     1    10     4     6    10
 8 IE          6     6     5     5     6     5     6     3     6     7     6     6     7     7     4
 9 OCEANIA     8     8     6    10     5    10     5     1    10     4     0     1    10     9    10
10 USA         3     2     2     7     3     1     2     0     8     3     3     1     0     8     8
# ... with 953 more rows,and 2 more variables: Q16 <int>,Q17 <int>

我想比较各个地区的答案,所以我先融化df,然后使用ggplot创建箱形图。

df1 ggplot(data = df1,aes(x =变量,y =值,填充=区域))+ geom_Boxplot()

不幸的是,由于有17个问题和13个地区,该箱线图异常繁忙,几乎让人难以理解。我如何简化它(例如仅绘制平均值和+/- 1标准误差),以便清晰易读。另外,我如何生成17个箱形图(每个问题一个,我确实需要全部17个问题),每组都可以看到13个区域?

真诚的

托马斯·飞利浦

解决方法

您可能想使用facet_wrap()。在这里,我使用一些简化的伪造数据来告诉您这个想法。

library(dplyr)
library(tidyr)
library(ggplot2)
set.seed(12234)
df <- data.frame(Region = sample(LETTERS[1:10],100,TRUE),Q1 = rpois(100,4),Q2 = rpois(100,3),Q3 = round(runif(100,1,10)),Q4 = round(runif(100,Q5 = round(10 * rnorm(100)))
df %>% pivot_longer(cols = -Region,names_to = "Question",values_to = "Value") %>%
  ggplot() +
  geom_boxplot(aes(x = Region,y = Value,fill = Region)) + 
  facet_wrap("Question")

faceted boxplots