分组(两次)和堆叠条形图,带有小平面环绕

grouped (twice) and stacked bar chart with facet wrapping

这是我的示例数据

mydata = data.frame (student =c("A","A","A","A","A","A","A","A","A",
"A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B",
"B","B","B","B","B","B"),
subject = c("His","His","His","His","His","His","His","His",
"Geo", "Geo","Geo","Geo","Geo","Geo","Geo","Geo","His","His","His","His","His","His",
 "His","His","Geo","Geo","Geo","Geo","Geo","Geo","Geo", "Geo"), 
year = c("2001","2001","2001","2001","2002","2002",
"2002","2002","2001","2001","2001","2001","2002","2002","2002","2002", "2001","2001","2001","2001","2002","2002","2002",
"2002", "2001","2001","2001","2001","2002","2002","2002","2002"), majortype=c("total", "total", "passed", 'passed',
"total", "total", "passed", 'passed',"total", "total", "passed", 'passed',
"total", "total", "passed", 'passed', "total", "total", "passed", 'passed',
"total", "total", "passed", 'passed',"total", "total", "passed", 'passed',
"total", "total", "passed", 'passed'),
 type=c("low income", "high income", "low income", "high income",
 "low income", "high income", "low income", "high income", "low income", 
"high income", "low income", "high income",  "low income", "high income",
 "low income", "high income", "low income", "high income", "low income", 
"high income", "low income", "high income", "low income", "high income",  
"low income", "high income", "low income", "high income", "low income", 
"high income", "low income", "high income"), 
 value = c(104,106,67,89,34, 67,12,56,97,56,67,45,123,134,100,111, 124,98,
100,90,78,90,65,80,123,78,100,98, 77,67,56,63))

我正在尝试实现堆叠(按主要类型)和分组(按年份和类型),然后按主题和学生进行分面包装。所以我有以下代码:

ggplot(mydata, aes(fill=majortype, y=value, x=year)) + 
  geom_bar(position="dodge", stat="identity") +
  facet_wrap(student~subject)+
  xlab("") + ylab("Number of students")+ labs(fill="")+
  theme_minimal() +
  theme(text = element_text(size=15),
        plot.title = element_text(size=20, face="bold"),
        axis.text  = element_text(size=9))

它几乎给了我想要的东西,但我真的很难在每个单独的条中放置低收入和高收入数量的堆叠条。理想情况下,我希望低收入的阴影更深,然后每组中的高收入人数在这里的颜色更浅。

我也尝试了以下代码,它按类型给出了堆叠,但我现在似乎无法按年份和主要类型对其进行分组。每年我都想要两个堆叠条,红色代表学生总数(按低收入和高收入堆叠),然后绿色代表通过的学生(按低收入和高收入堆叠)。

ggplot(mydata, aes(fill=type, y=value, x=year)) + 
  geom_bar(position="stack", stat="identity") +
  facet_wrap(student~subject)+
  xlab("") + ylab("Number of students")+ labs(fill="")+
  theme_minimal() +
  theme(text = element_text(size=15),
        plot.title = element_text(size=20, face="bold"),
        axis.text  = element_text(size=9))

如有任何帮助,我们将不胜感激!如果它有帮助,我希望创建这样的东西:

根据 OP 关于想要闪避和堆叠条的评论更新:被 majortype 闪避;按 type.

堆叠

组合闪避条和堆叠条不是 ggplot 的功能:https://github.com/tidyverse/ggplot2/issues/2267

但是,在这个 link 的帮助下:ggplot2 - bar plot with both stack and dodge 和一些额外的修补你可以试试这个...

library(ggplot2)
library(dplyr)

# prepare data so that values are in effect stacked and in the right order

dat <- 
  mydata %>% 
  group_by(year, subject, student, majortype) %>% 
  arrange(type) %>% 
  mutate(val_cum = cumsum(value))

ggplot(dat, aes(fill = majortype, y = val_cum, x = year)) +
  geom_col(data = filter(dat, type == "low income"), position = position_dodge2(width = 0.9), alpha = 0.5)+
  geom_col(data = filter(dat, type == "high income"), position = position_dodge2(width = 0.9), alpha = 1) +
  geom_tile(aes(y = NA_integer_, alpha = type)) +
  scale_fill_manual(breaks = c("passed", "total"),
                    labels = c("High income - passed", "High income - total"),
                    values = c("red", "blue"))+
  guides(alpha = guide_legend(override.aes = list(fill = c("red", "blue"), alpha = c(0.5, 0.5))))+
  scale_alpha_manual(breaks = c("high income", "low income"),
                    labels = c("Low income - passed", "Low income - total"),
                    values = c(1, 0.5))+
  facet_wrap(student~subject)+
  labs(x = NULL, 
       y = "Number of students",
       fill = NULL,
       alpha = NULL)+
  theme_minimal() +
  theme(text = element_text(size=15),
        plot.title = element_text(size=20, face="bold"),
        axis.text  = element_text(size=9))

reprex package (v2.0.1)

于 2022-05-09 创建