简体   繁体   English

如何在 R 中使用 ggplot 在堆积条形图 plot 上显示百分比值

[英]How display percent values on a stacked bar plot using ggplot in R

I've produced this graph (on the attached photo) using ggplot2.我使用 ggplot2 制作了这张图(在所附照片上)。

My code goes like this:我的代码是这样的:

ggplot(data, aes(fill=condition, y=value, x=period)) + 
geom_bar(position="fill", stat="identity")+
xlab("Période") +  ylab("Pourcentage") +
ggtitle("Répartition des français et étrangers")+
scale_y_continuous(labels = scales::percent)+
scale_fill_brewer(palette = "OrRd")

Result looks like this结果看起来像这样

I just need to display the values on the bars.我只需要在条上显示值。 However, adding geom_text is not straightforward.但是,添加 geom_text 并不简单。 I would appreciate it if you could provide some ideas.如果您能提供一些想法,我将不胜感激。

Without knowing the data:在不知道数据的情况下:

Try: geom_text(aes(label = paste0(value*100,"%")), position = position_stack(vjust = 0.5), size = 2)尝试: geom_text(aes(label = paste0(value*100,"%")), position = position_stack(vjust = 0.5), size = 2)

ggplot(data, aes(fill=condition, y=value, x=period)) + 
  geom_bar(position="fill", stat="identity")+
  geom_text(aes(label = paste0(value*100,"%")), 
            position = position_stack(vjust = 0.5), size = 2)
  xlab("Période") +  ylab("Pourcentage") +
  ggtitle("Répartition des français et étrangers")+
  scale_y_continuous(labels = scales::percent)+
  scale_fill_brewer(palette = "OrRd")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM