【发布时间】:2018-11-04 07:46:11
【问题描述】:
我正在尝试使用 ggplot2 创建一个条形图,显示 y 轴上的计数,以及每个条形顶部的总数百分比。我已经计算了总数和百分比,但无法弄清楚如何在条形顶部添加百分比总数。我正在尝试使用 geom_text,但无法正常工作。
一个最小的例子:
iris %>%
group_by(Species) %>%
summarize(count = n()) %>%
mutate(percent = count/sum(count)) %>%
ggplot(aes(x=Species, y=count)) +
geom_bar(stat="identity") +
geom_text(aes(label = scales::percent(..prop..), y=..count..), stat= "count", vjust = -.5)
我查看了其他答案,例如 How to add percentage or count labels above percentage bar plot?,但在这些示例中,y 轴和标签都显示百分比。我正在尝试在标签中显示 y 轴上的计数和百分比。
【问题讨论】:
-
你见过this question吗?
-
是的,我看过那些,但是那些在 y 轴和标签上都显示了百分比。我正在寻找 y 轴上的计数和标签上的百分比。
标签: r ggplot2 dplyr tidyr tidyverse