繁体   English   中英

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

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

我使用 ggplot2 制作了这张图(在所附照片上)。

我的代码是这样的:

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")

结果看起来像这样

我只需要在条上显示值。 但是,添加 geom_text 并不简单。 如果您能提供一些想法,我将不胜感激。

在不知道数据的情况下:

尝试: 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