小编Hye*_*ong的帖子

如何使用 ggplot geom_bar 显示堆叠列中的百分比?

我正在尝试在堆积条形图中添加百分比标签。我可以在 geom_bar 中添加什么来显示堆积条形图内的百分比标签?

这是我的数据:

myresults=data.frame(
    manipulation=rep(c(-20,-10,0,10,20,-20,-10,0,10,20,-20,-10,0,10,20)),
    variable=rep(c("a","a","a","a","a","f","f","f","f","f","l","l","l","l","l")),
    value=c(73,83,76,75,78,261,301,344,451,599,866,816,780,674,523))
Run Code Online (Sandbox Code Playgroud)

这是我的条形图,没有百分比标签。

我对此了解甚少。我用谷歌搜索“gglot stacked bar百分比标签”,发现添加百分比标签可以用“+ geom_text(stat=”count”)”来完成。

但是当我将 + geom_text(stat="count") 添加到我的 ggplot geom_bar 时,R 说“错误:stat_count() 不得与 ay 美学一起使用。” 我试图弄清楚什么是审美,但不太成功。

这就是我所做的:

mydata <- ggplot(myresults, aes(x=manipulation, y=value, fill=variable))

mydata + geom_bar(stat="identity", position="fill", colour="black") + scale_fill_grey() + scale_y_continuous(labels=scales::percent) + theme_bw(base_family="Cambria") + labs(x="Manipulation", y=NULL, fill="Result") + theme(legend.direction="vertical", legend.position="right")
Run Code Online (Sandbox Code Playgroud)

r ggplot2 geom-bar

3
推荐指数
1
解决办法
8605
查看次数

标签 统计

geom-bar ×1

ggplot2 ×1

r ×1