分组(两次)和堆叠的条形图,带有刻面包装
这是我
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))
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))
Any help would be appreciated! If it helps at all, I am looking to create something like this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新了OP关于想要躲避和堆叠的条的评论:由
majortype
躲避;由类型
堆叠。组合躲避和堆叠的条不是GGPLOT的功能: https://github.com/tidyyverse/ ggplot2/desiss/2267
然而,在此链接的帮助下: ggplot2-带有堆栈和道奇的酒吧图以及一些其他修补,您可以尝试一下...
由
Updated following OP's comment about wanting dodged and stacked bars: dodged by
majortype
; stacked bytype
.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...
Created on 2022-05-09 by the reprex package (v2.0.1)