【问题标题】:How to show percentages in stacked column with ggplot geom_bar? [duplicate]如何使用ggplot geom_bar在堆积列中显示百分比? [复制]
【发布时间】:2019-11-07 08:17:44
【问题描述】:

我正在尝试在堆积条形图中添加百分比标签。我可以添加什么到我的 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))

This is my bar chart, without percentage labels.

我对此知之甚少。我搜索了“gglot 堆积条百分比标签”,发现可以使用“+ geom_text(stat="count")”添加百分比标签。

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

这就是我所做的:

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

【问题讨论】:

  • 欢迎来到 SO。我将您的问题标记为重复问题以帮助您 - 该问题应该能够帮助您。如果您觉得它没有回答您的问题,请更改您的问题并告诉我。
  • @Tjebo 感谢您阅读并抽出宝贵时间发表评论!但是你为我链接的问题和答案是我得到的谷歌结果,这让我尝试“+ geom_text(stat="count")”。我想知道我的情况出了什么问题。

标签: r ggplot2 geom-bar


【解决方案1】:

您可以执行类似于Adding percentage labels to a bar chart in ggplot2 中接受的答案的操作。主要区别在于您的值是堆积的(“stacked”),而在该示例中它是并排的(“dodged”)

制作一列百分比:

myresults_pct <- myresults %>% 
group_by(manipulation) %>% 
mutate(pct=prop.table(value))

现在我们绘制这个:

    ggplot(myresults_pct, 
aes(x=manipulation, y=pct,fill=variable)) + 
geom_col()+
scale_fill_grey()+
geom_text(aes(label = scales::percent(pct)),
position="stack",vjust=+2.1,col="firebrick",size=3)+
scale_y_continuous(label = scales::percent)

geom_text 中的重要参数是 position="stacked",并使用 vjust 来上下移动标签。 (我提前为糟糕的文字颜色道歉..)。

【讨论】:

  • 不客气!我鼓励您使用 dput 提供数据,这是提出一个好问题的一部分,以便其他人也可以提供帮助和学习:)
  • 谢谢!我编辑了我的问题以添加我使用的数据,但我的数据框最初是由一个非常大的 excel 文件通过执行 dcast(myresults,操作〜结果)然后融化(myresults,id.vars =“manipulation”)制成的。发表此评论时,我突然注意到 5 小时前我没有意识到的事情。我可以问一个后续问题吗?似乎我确实需要 position="fill",因为当我删除它时,geom_text 工作,但我的 y 轴变为 125 000%、100 000%、75 000%、50 000%、25 000%、0% 而不是原来的 100~0%。如果我想保留 position="fill" 并仍然添加标签,该怎么办?
  • 感谢您的提供,我可以根据您提供的内容更改我上面的帖子
  • 完成@Hyeonjeong,下次请提供数据,是的,您不需要 position = "fill",这真的很奇怪。
  • 嗨@Hyeonjeong 有用吗?
【解决方案2】:

您可以尝试创建geom文本的位置并将其放在条上:

mydata[, label_ypos := cumsum(value), by = manipulation]

ggplot(myresults, aes(x=manipulation, y=value, fill=variable)) + 
geom_bar(stat="identity", position="fill", colour="black") +
geom_text(aes(y=label_ypos, label= paste(round(rent, 2), '%')), vjust=2, 
        color="white", size=3.5) +
scale_y_continuous(labels = scales::percent) +
labs(x = "Manipulation", y=NULL, fill="Result") +
theme_bw(base_family = "Cambria") +
theme(legend.direction = "vertical", legend.position = "right") +
scale_fill_grey() 

【讨论】:

  • 感谢您的回答! StupidWolf 的回答解决了我的问题,但我从你的回答中得到了意想不到的额外帮助(特别是 "color="white", size=3.5" 部分),这使我的条形图更漂亮。
猜你喜欢
  • 2017-09-15
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
相关资源
最近更新 更多