【发布时间】:2020-04-19 17:32:45
【问题描述】:
我想为百分比条形图添加百分比标签
我通过position="fill" (Add percentage labels to a stacked barplot) 和这里 (How to draw stacked bars in ggplot2 that show percentages based on group?) 找到了解决方案,但是,我想为每个组保留相对频率。
这是一个示例图:
# library
library(ggplot2)
# data
df <- data.frame(group=c("A","A","A","A","B","B","B","C","C"),
anon=c("yes","no","no","no","yes","yes","no","no","no"))
# percentage barplot
ggplot(df, aes(group),fill=anon) +
geom_bar(aes(y = (..count..)/sum(..count..),fill=anon)) +
scale_y_continuous(labels=scales::percent) +
ylab("relative frequencies")
由reprex package (v0.3.0) 于 2020 年 4 月 19 日创建
现在我想为每个条形的每个红色和绿色部分添加百分比标签,以便获得“相对相对”(例如,A 组的“是”为 25%)值。
如何才能做到这一点?我是否必须为此更改我的 df 或者这在 ggplot 函数中是否有可能
【问题讨论】:
标签: r ggplot2 percentage