如何获取几何条百分比以反映组内百分比而不是整体百分比

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

我在 R 中有一个数据集,如下所示:

'f' is the dataset, column 'e' (character) is the group variable, column 'd' (factor) is the data

使用以下代码,我得到下图:

ggplot(subset(f,!is.na(d)),aes(x=d,y=(..count..)/sum(..count..),fill=forcats::fct_rev(e))) + 
 geom_bar(position="dodge") + 
 scale_y_continuous(labels = scales::percent) +
 theme(panel.grid.major.y = element_line(color="gray"),
    panel.background =element_blank(), axis.line=element_line("black"), axis.text.x=element_text(face="bold"), 
    legend.title=element_blank(), axis.title.y=element_blank(), axis.title.x=element_blank(),
    plot.title = element_text(hjust = 0.5)
 ) +
 scale_x_discrete(
   labels=c('$0', '$1 to $10K', 'Over $10K to $20K', 'Over $20K to $30K','Over $30K to $40K','Over $40K to $50K','Over $50K')
 ) +
 geom_text(aes(label = scales::percent(round((..count..)/sum(..count..),2)),y= ((..count..)/sum(..count..))), stat="count", position = position_dodge(width = .9),vjust = -1)
 labs(title="Perceived Debt Before and After CSP 2024") + 
 scale_fill_manual(values=c("darkblue","lightblue"))

Image

如您所见,两个组的百分比相加为 100%,但我真正希望组内的百分比相加为 100%(CSP 前和 CSP 后)。关于如何做到这一点有什么想法吗?

注意:x 轴已根据数据重新标记。也就是说,“d”列中的 1 表示 $0,“d”列中的 2 表示“$1 到 $10,000”,依此类推。

r ggplot2 bar-chart geom-bar geom-text
1个回答
0
投票

未经测试,因为我没有数据,但类似的东西应该有效。 您可能最好自己聚合数据,然后在调用

stat="identity"
时使用
geom_bar()
绘制聚合数据。

data %>% 
group_by(e, d) %>% 
tally() %>%
group_by(e) %>% 
mutate(pct = n/sum(n)) %>% 
ggplot(aes(x=d,y=pct,fill=forcats::fct_rev(e))) + 
 geom_bar(stat = "identity", position="dodge") + 
 scale_y_continuous(labels = scales::percent) +
 theme(panel.grid.major.y = element_line(color="gray"),
    panel.background =element_blank(), 
    axis.line=element_line("black"), 
    axis.text.x=element_text(face="bold"), 
    legend.title=element_blank(), 
    axis.title.y=element_blank(), 
    axis.title.x=element_blank(),
    plot.title = element_text(hjust = 0.5)
 ) +
 scale_x_discrete(
   labels=c('$0', '$1 to $10K', 'Over $10K to $20K', 'Over $20K to $30K','Over $30K to $40K','Over $40K to $50K','Over $50K')
 ) +
 geom_text(aes(label = scales::percent(round(pct, 2)),y= round(pct, 2)), 
                position = position_dodge(width = .9),
                vjust = -1) + 
 labs(title="Perceived Debt Before and After CSP 2024") + 
 scale_fill_manual(values=c("darkblue","lightblue"))
© www.soinside.com 2019 - 2024. All rights reserved.