我如何在构面中显示组内百分比?

问题描述 投票:0回答:1

我正在尝试根据学生以前的教育程度来想象他们辍学的趋势。我有两个感兴趣的变量; “ gymnasiegrov”显示他们的高中课程,“ totstatus_tri”显示结果,并具有三个级别(退出第一学期,随后退出,仍在课程中)。

到目前为止,我已经设法使用以下代码进行了不错的可视化:

ggplot(fulldata, aes(x=fct_infreq(gymnasiegrov))) + 
  geom_bar()+
  coord_flip() +
  scale_x_discrete(limits = rev(levels(fct_infreq(fulldata$gymnasiegrov))))+
  facet_wrap(~totstatus_tri)

然后我得到以下结果:

enter image description here

这是很好的可视化,但是来自某些程序的学生比其他程序的“很多”。这使得模式难以区分。我更希望看到每个程序的组内百分比,以便每个结果在各个方面的总和达到100%。

例如,最大的程序(Ekonomiprogrammet / Ekonomi)总共有55名学生,因此第一个方面中最上面的栏应为34/55,第二个方面的栏应为15/55,第三个方面的栏应为6 / 55等。

这可能吗?

r ggplot2 facet-wrap
1个回答
0
投票

也许是这样(在antal是您的count变量)?

library(tidyverse)
fulldata %>% 
    group_by(gymnasiegrov) %>% 
    mutate(andel = antal / sum(antal)) %>% 
    ggplot(.) + 
    geom_bar(mapping = aes(x = gymnasiegrov, y = andel)) + 
    coord_flip() + 
    facet_wrap(~totstatus_tri)
© www.soinside.com 2019 - 2024. All rights reserved.