如何在rstudio中绘制堆叠和分组的棒块图?
我已经创建了一个分组的Barchart,该Barchart显示了按年龄组和性别分组的客户端数(n = 25)。但是现在,我需要包括所有筛选研究以显示数据质量的客户(n = 52)。它应该看起来像附着的Barchart,但总数以较浅的颜色堆叠,因此可以看到:在年龄组1中,我们筛选了15位女性和9名男性,但最终只有8位女性注册了学习。我希望这样可以理解。我将添加现有的条形图。
ggplot(data=daten, aes(x=AG, y=stat(count), group=factor(GESCHLECHT), fill=factor(GESCHLECHT)))+
geom_bar(position=position_dodge2(preserve="single", padding=0))+
scale_fill_manual(values = c("steelblue","darkred"), labels=c("Männlich", "Weiblich"))+
labs(y="Anzahl", x="Altersgruppen", fill="Geschlecht:")+
scale_x_continuous(breaks=seq(1,3, by=1), labels=c("25 bis <51","51 bis <65", ">65"))+
scale_y_continuous(limits=c(0, 10))+
geom_text(aes(label=stat(count)),stat="count", vjust=-0.5, position=position_dodge(width = 1))+
theme(legend.position="bottom")
我不知道如何组织我的数据,以便我可以做这样的事情。最后,应该看起来像第一个栏上方(n = 8),只有较浅的颜色才能继续,然后用n = 6个女性说(最终表明我们筛选了15在该年龄组中总共是女性)
我认为首先,我需要使用所有n = 52个客户端创建一个数据框架,并具有针对年龄组和性别的代码。但是,如何将杠铃分开,以使筛选和招募客户之间的区别变得清晰?
I already created a grouped barchart, that shows the number of clients (n=25) grouped by agegroup and gender. But now I need to include all clients (n=52) that were screened for the study to show quality of data. It should look like the attached barchart, but with the total number stacked in a lighter color, so one can see: In Age Group 1, we screened for example 15 females and 9 males, but in the end only 8 females signed up for the study. I hope it is understandable like this. I'll add the existing bar chart.
the code for this chart is the following:
ggplot(data=daten, aes(x=AG, y=stat(count), group=factor(GESCHLECHT), fill=factor(GESCHLECHT)))+
geom_bar(position=position_dodge2(preserve="single", padding=0))+
scale_fill_manual(values = c("steelblue","darkred"), labels=c("Männlich", "Weiblich"))+
labs(y="Anzahl", x="Altersgruppen", fill="Geschlecht:")+
scale_x_continuous(breaks=seq(1,3, by=1), labels=c("25 bis <51","51 bis <65", ">65"))+
scale_y_continuous(limits=c(0, 10))+
geom_text(aes(label=stat(count)),stat="count", vjust=-0.5, position=position_dodge(width = 1))+
theme(legend.position="bottom")
I have no idea how to organize my data so that I could do something like that. In the end it should look like that above the first bar (n=8) the bar should continue, only with for example a lighter color and then lets say with n=6 more females (in the end that would show that we screened 15 females in total in that agegroup)
I think first I would need to create a data frame with all n=52 clients, with a code for Age Group and gender. But how can I divide the bars so that the difference between screened and recruited clients become clear?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西吗?
由 reprex 包 (v2.0.0)
Something like this?
Created on 2022-04-13 by the reprex package (v2.0.0)