将百分比标签添加到堆叠的条形图中 [英] Add percentage labels to a stacked barplot

查看:136
本文介绍了将百分比标签添加到堆叠的条形图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地在R中制作了一个堆积条形图,其中几个不同类别的百分比总计达到100%.我在这里做了一个示例数据框.

I have successfully made a stacked barplot in R where the percentages add up to 100% for several different categories. I made an example dataframe here.

example.Category<- c("Cat1","Cat2","Cat3","Cat4","Cat5","Cat6")
percent.good <- c(.25,.29,.45,.5,.8,.82)
example.data <- data.frame(example.Category,percent.good)
example.data$percent.bad <- (1-example.data$percent.good)

数据框如下所示.

     example.Category percent.good percent.bad
1             Cat1         0.25        0.75
2             Cat2         0.29        0.71
3             Cat3         0.45        0.55
4             Cat4         0.50        0.50
5             Cat5         0.80        0.20
6             Cat6         0.82        0.18

然后我使用了重塑包装中的熔体来得到这个...

I then used melt from the reshape package to get this...

example.melt <- melt(example.data, id.vars="example.Category")

   example.Category     variable value
1              Cat1 percent.good  0.25
2              Cat2 percent.good  0.29
3              Cat3 percent.good  0.45
4              Cat4 percent.good  0.50
5              Cat5 percent.good  0.80
6              Cat6 percent.good  0.82
7              Cat1  percent.bad  0.75
8              Cat2  percent.bad  0.71
9              Cat3  percent.bad  0.55
10             Cat4  percent.bad  0.50
11             Cat5  percent.bad  0.20
12             Cat6  percent.bad  0.18

然后我使用ggplot制作了一个显示这些百分比的堆叠条形图.

Then I used ggplot to make a stacked barplot displaying these percentages.

ggplot(example.melt, aes(x=example.Category, y=value, fill = variable)) +
  geom_bar(position = "fill", stat = "identity",color='black',width=0.9) +
  scale_y_continuous(labels = scales::percent) +
  geom_text(aes(label = paste0((example.data$percent.good && example.data$percent.bad)*100), '%'),position = position_dodge(width = .9),size = 3)

这将产生此图,这是我想要的,除了它标记的方式.

This yields this graph, which is what I want, except for the way it labels.

我想做的是在每个堆叠的条形上为每种颜色设置百分比标签,我不知道该怎么做,并且确定自己做得不好.我所要做的就是以某种方式创建另一个大约100的类别.我如何使百分比标签显示在条形图的每个部分的图形上?

What I want to do is have percentage labels for each color on each stacked bar, and I have no idea how to do that and am certain that I didn't do it right. All I managed to do was somehow create another category that has some '100's around. How would I get the percent labels to appear on this graph for each part of the bar?

我希望这不是多余的.谢谢.

I hope this isn't redundant/asked before. Thanks.

推荐答案

您可以执行以下操作...

You could do something like this...

#set positions for labels
example.melt$labelpos <- ifelse(example.melt$variable=="percent.bad",
                         example.melt$value/2, 1 - example.melt$value/2)
ggplot(example.melt, aes(x=example.Category, y=value, fill = variable)) +
  geom_bar(position = "fill", stat = "identity",color='black',width=0.9) +
  scale_y_continuous(labels = scales::percent) +
#use positions to plot labels
  geom_text(aes(label = paste0(100*value,"%"),y=labelpos),size = 3)

这篇关于将百分比标签添加到堆叠的条形图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆