如何将堆积百分比条形图标签居中 [英] How to center stacked percent barchart labels

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

问题描述

我正在尝试使用 ggplot2 绘制漂亮的 堆积百分比条形图.我已经阅读了一些材料并且几乎设法绘制出我想要的图.另外,我附上了材料,它可能在一个地方有用:

您可以在上图中看到问题 - 标签看起来很糟糕,而且彼此重叠.

我现在正在寻找的是:

  1. 如何在条形(区域)中间放置标签

  2. 如何不绘制所有标签,但例如绘制大于 10% 的标签?

  3. 如何解决重叠问题?

对于 Q 1. @MikeWise 建议可能的

<小时>

对于旧版本,我们需要计算位置.(同上,但多了一行定义 pos):

# 计算百分比和标签位置df.summary = df %>% group_by(Brand, Category) %>%summarise(USD = sum(USD)) %>% # 在每个品牌内,对每个类别中的所有值求和变异(百分比 = 美元/总和(美元),pos = cumsum(百分比) - 0.5*百分比)

然后使用 ifelse 语句绘制数据以确定是否绘制了标签.在这种情况下,我避免为小于 7% 的百分比绘制标签.

ggplot(df.summary, aes(x=reorder(Brand,USD,function(x)+sum(x)), y=percent, fill=Category)) +geom_bar(stat='identity', width = .7, colour="black", lwd=0.1) +geom_text(aes(label=ifelse(percent >= 0.07, paste0(sprintf("%.0f", percent*100),"%"),""),y=pos), colour="white") +coord_flip() +scale_y_continuous(labels = percent_format()) +实验室(y="", x="")

I am trying to plot nice stacked percent barchart using ggplot2. I've read some material and almost manage to plot, what I want. Also, I enclose the material, it might be useful in one place:

How do I label a stacked bar chart in ggplot2 without creating a summary data frame?

Create stacked barplot where each stack is scaled to sum to 100%

R stacked percentage bar plot with percentage of binary factor and labels (with ggplot)

My problem is that I can't place labels where I want - in the middle of the bars.

You can see the problem in the picture above - labels looks awfull and also overlap each other.

What I am looking for right now is:

  1. How to place labels in the midde of the bars (areas)

  2. How to plot not all the labels, but for example which are greather than 10%?

  3. How to solve overlaping problem?

For the Q 1. @MikeWise suggested possible solution. However, I still can't deal with this problem.

Also, I enclose reproducible example, how I've plotted this grahp.

library('plyr')
library('ggplot2')
library('scales')
set.seed(1992)
n=68

Category <- sample(c("Black", "Red", "Blue", "Cyna", "Purple"), n, replace = TRUE, prob = NULL)
Brand <- sample("Brand", n, replace = TRUE, prob = NULL)
Brand <- paste0(Brand, sample(1:5, n, replace = TRUE, prob = NULL))
USD <- abs(rnorm(n))*100

df <- data.frame(Category, Brand, USD)

# Calculate the percentages
df = ddply(df, .(Brand), transform, percent = USD/sum(USD) * 100)


# Format the labels and calculate their positions
df = ddply(df, .(Brand), transform, pos = (cumsum(USD) - 0.5 * USD))

#create nice labes
df$label = paste0(sprintf("%.0f", df$percent), "%")  



ggplot(df, aes(x=reorder(Brand,USD,
                              function(x)+sum(x)),  y=percent, fill=Category))+
  geom_bar(position = "fill", stat='identity',  width = .7)+
  geom_text(aes(label=label, ymax=100, ymin=0), vjust=0, hjust=0,color = "white",  position=position_fill())+
  coord_flip()+
  scale_y_continuous(labels = percent_format())+
  ylab("")+
  xlab("")

解决方案

Here's how to center the labels and avoid plotting labels for small percentages. An additional issue in your data is that you have multiple bar sections for each colour. Instead, it seems to me all the bar sections of a given colour should be combined. The code below uses dplyr instead of plyr to set up the data for plotting:

library(dplyr)

# Initial data frame   
df <- data.frame(Category, Brand, USD)

# Calculate percentages
df.summary = df %>% group_by(Brand, Category) %>% 
  summarise(USD = sum(USD)) %>%   # Within each Brand, sum all values in each Category
  mutate(percent = USD/sum(USD))

With ggplot2 version 2, it is no longer necessary to calculate the coordinates of the text labels to get them centered. Instead, you can use position=position_stack(vjust=0.5). For example:

ggplot(df.summary, aes(x=reorder(Brand, USD, sum), y=percent, fill=Category)) +
  geom_bar(stat="identity", width = .7, colour="black", lwd=0.1) +
  geom_text(aes(label=ifelse(percent >= 0.07, paste0(sprintf("%.0f", percent*100),"%"),"")),
                position=position_stack(vjust=0.5), colour="white") +
  coord_flip() +
  scale_y_continuous(labels = percent_format()) +
  labs(y="", x="")


With older versions, we need to calculate the position. (Same as above, but with an extra line defining pos):

# Calculate percentages and label positions
df.summary = df %>% group_by(Brand, Category) %>% 
  summarise(USD = sum(USD)) %>%   # Within each Brand, sum all values in each Category
  mutate(percent = USD/sum(USD),
         pos = cumsum(percent) - 0.5*percent)

Then plot the data using an ifelse statement to determine whether a label is plotted or not. In this case, I've avoided plotting a label for percentages less than 7%.

ggplot(df.summary, aes(x=reorder(Brand,USD,function(x)+sum(x)), y=percent, fill=Category)) +
  geom_bar(stat='identity',  width = .7, colour="black", lwd=0.1) +
  geom_text(aes(label=ifelse(percent >= 0.07, paste0(sprintf("%.0f", percent*100),"%"),""),
                y=pos), colour="white") +
  coord_flip() +
  scale_y_continuous(labels = percent_format()) +
  labs(y="", x="")

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

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