简体   繁体   English

如何将计数标签添加到 R ggplot 中的百分比条形图?

[英]How do I add count labels to a percentage barplot in R ggplot?

Suppose we want to use R ggplot to produce a barplot showing percentages of some category variable across another category variable.假设我们想使用 R ggplot 生成一个条形图,显示某个类别变量在另一个类别变量中的百分比。 Below is a small data frame in R and some code to get a nice pretty barplot of what I want.下面是 R 中的一个小数据框和一些代码来获得我想要的漂亮的条形图。

MYDATA <- data.frame(Region = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5),
                     Sex    = c("M", "F", "M", "F", "M", "F", "M", "F", "M", "F"),
                     Count  = c(185, 130, 266, 201, 304, 283, 102, 60, 55, 51))

library(ggplot2)

ggplot(data = MYDATA, aes(x = Region, y = Count, fill = Sex)) + 
       geom_bar(position = "fill", stat = 'identity') + 
       scale_y_continuous(labels = scales::percent) +   
       scale_fill_manual(values = c("Maroon1", "RoyalBlue1")) +
       ggtitle("Figure 1: Sex breakdown by Region") +
       xlab("Region") + ylab("Percentage")

在此处输入图像描述

Now, I would like to supplement this bar plot by adding text that displays the raw count values inside the bars.现在,我想通过添加在条形图内显示原始计数值的文本来补充此条形图。 For the females, I would like the counts to be displayed at the top of the graph, inside the pink bars.对于女性,我希望计数显示在图表顶部的粉红色条内。 For the males, I would like the counts to be displayed at the bottom of the graph, inside the blue bars.对于男性,我希望计数显示在图表底部的蓝色条内。 I have tried using geom_text but I cannot figure out how to place the labels where I want them.我试过使用geom_text但我不知道如何将标签放在我想要的地方。 Instead, my attempts have generally led to the count values being placed at their own values, which destroys the scale of the percentages in the plots.相反,我的尝试通常导致将计数值置于它们自己的值,这破坏了图中百分比的比例。 Can any of you learned people tell me how to add the raw count values as text in the bars without ruining the rest of the plot?你们中有没有学过的人能告诉我如何在不破坏情节的其余部分的情况下将原始计数值作为文本添加到条形图中?

Okay, so after posting this question I figured out a solution.好的,所以在发布这个问题之后我想出了一个解决方案。 Despite answering my own question (sorry guys) I will leave this post up in case anyone has the same problem they need to solve.尽管回答了我自己的问题(抱歉,伙计们),但我还是会留下这篇文章,以防有人遇到他们需要解决的相同问题。 Please feel free to add other solutions also.请随时添加其他解决方案。

The trick that eventually worked was to break the problem down to specify separate labels for each sex using two calls to geom_text with an ifelse command to enter labels only for one sex at a time, and manual y-positioning at the desire height in each statement.最终奏效的技巧是将问题分解为使用ifelse命令两次调用geom_text为每个性别指定单独的标签,一次只为一个性别输入标签,并在每个语句中手动 y 定位到期望高度. Here is the code and plot:这是代码和情节:

ggplot(data = MYDATA, aes(x = Region, y = Count, fill = Sex)) + 
       geom_bar(position = "fill", stat = 'identity') + 
       geom_text(aes(label = ifelse(Sex == "F", Count, "")), y = 0.95) +
       geom_text(aes(label = ifelse(Sex == "M", Count, "")), y = 0.05) +
       scale_y_continuous(labels = scales::percent) +   
       scale_fill_manual(values = c("Maroon1", "RoyalBlue1")) +
       ggtitle("Figure 1: Sex breakdown by Region") +
       xlab("Region") + ylab("Percentage");

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM