分组(两次)和堆叠的条形图,带有刻面包装

发布于 2025-01-26 18:36:24 字数 3122 浏览 2 评论 0原文

这是我

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

试图实现的示例数据(由Majortype),并分组(按年和类型)进行分组,然后由学科和学生包裹。因此,我有以下代码:

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

”在此处输入图像描述”

它几乎给了我我想要的东西,但是我确实在努力将低收入和高收入数量的堆叠栏放入每个单独的酒吧内。理想情况下,我很想让低收入是一种深色的阴影,然后每个群体中的高收入数量是这些颜色的较浅阴影。

我也尝试了以下代码,这给了我类型的堆叠,但是现在我似乎无法按年和Majortype进行分组。每年,我都想要两个堆叠的酒吧,为总学生提供红色(由低收入与高收入堆叠),然后绿色的学生(由低收入与高收入堆积)。

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

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

Here is my sample data

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

I am trying to achieve a stacked (by majortype) and grouped (by year and type) and then facet wrapped by subject and student. So I have the following code:

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

enter image description here

It pretty much gives me what I want, but Im really struggling to put stacked bars of the number of low income and high income within each individual bar. Ideally I would love to have low income be a darker shade and then the number of high income within each group be a lighter shade of these colors here.

I tried the following code too, which gives me the stacked by type, but I cant seem to now group this by year AND majortype. For each year I would like two stacked bars, red for total students (stacked by low vs high income) and then green for passed students (stacked by low vs high income).

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

enter image description here

Any help would be appreciated! If it helps at all, I am looking to create something like this:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2025-02-02 18:36:24

更新了OP关于想要躲避和堆叠的条的评论:由majortype躲避;由类型堆叠。

组合躲避和堆叠的条不是GGPLOT的功能: https://github.com/tidyyverse/ ggplot2/desiss/2267

然而,在此链接的帮助下: ggplot2-带有堆栈和道奇的酒吧图以及一些其他修补,您可以尝试一下...

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

“

Updated following OP's comment about wanting dodged and stacked bars: dodged by majortype; stacked by type.

Combining dodged and stacked bars is not a feature of ggplot: https://github.com/tidyverse/ggplot2/issues/2267

However, with help from this link: ggplot2 - bar plot with both stack and dodge and a bit of additional tinkering you could try this...

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

Created on 2022-05-09 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文