将百分比标签添加到堆积条形图ggplot2

时间:2017-12-22 05:10:38

标签: r ggplot2 labels

我坚持在ggplot2中创建图表。我尝试使用百分比创建堆积条形图,类似于此页面上的图表,但我很难在条形图中添加百分比标签:How to draw stacked bars in ggplot2 that show percentages based on group?

我发现尝试添加百分比标签的所有答案都使用与代码类似的内容

  

geom_text(aes(label = label),position = position_stack(vjust = 0.5),   size = 2)

但它不适合我。

我的数据如下:

County  Group   Plan1   Plan2   Plan3   Plan4   Plan5   Total
County1 Group1  2019    597 513 5342    3220    11691
County2 Group1  521 182 130 1771    731 3335
County3 Group1  592 180 126 2448    1044    4390
County4 Group1  630 266 284 2298    937 4415
County5 Group1  708 258 171 2640    1404    5181
County6 Group1  443 159 71  1580    528 2781
County7 Group1  492 187 157 1823    900 3559
County8 Group1  261 101 84  1418    357 2221

我没有百分比的图表如下所示: graph1

代码:

melt(df[df$Group=="Group1",],measure.vars = c("Plan1","Plan2","Plan3","Plan4", "Plan5"),variable.name = "Counties",value.name = "value") %>% 
ggplot(aes(x=County,y=value,fill=Counties))+
  geom_bar(stat = "identity",position="fill", color="black", width=0.9) + 
  labs(y="Percent", fill="Plan Type") + ylab("Percentage") + coord_flip() + scale_y_continuous(labels=scales::percent)

使用上面的geom_text()代码后,它变成了这个烂摊子: graph2

代码:

melt(df[df$Group=="Group1",],measure.vars = c("Plan1","Plan2","Plan3","Plan4", "Plan5"),variable.name = "Counties",value.name = "value") %>% 
ggplot(aes(x=County,y=value,fill=Counties))+
  geom_bar(stat = "identity",position="fill", color="black", width=0.9) + 
  labs(y="Percent", fill="Plan Type") + ylab("Percentage") + coord_flip() + scale_y_continuous(labels=scales::percent)+ 
geom_text(aes(label=paste0(round(value/100),"%")), position=position_stack(vjust=0.5))

有什么建议吗?非常感谢任何建议/指导!谢谢!!

1 个答案:

答案 0 :(得分:1)

您的方法不起作用,因为标签不是%,而是原始值。你必须自己做统计:

df <- read.table(text="County  Group   Plan1   Plan2   Plan3   Plan4   Plan5   Total
County1 Group1  2019    597 513 5342    3220    11691
                 County2 Group1  521 182 130 1771    731 3335
                 County3 Group1  592 180 126 2448    1044    4390
                 County4 Group1  630 266 284 2298    937 4415
                 County5 Group1  708 258 171 2640    1404    5181
                 County6 Group1  443 159 71  1580    528 2781
                 County7 Group1  492 187 157 1823    900 3559
                 County8 Group1  261 101 84  1418    357 2221", header = TRUE)

library(tidyverse)
df %>% 
  filter(Group == "Group1") %>% 
  select(-Total) %>% 
  gather(key = `Plan Type`, value = value, -County, -Group) %>% 
  group_by(County, Group) %>% 
  mutate(Percentage = value/sum(value)) %>% 
  ggplot(aes(x = County, y = Percentage, fill = `Plan Type`, label = paste0(round(Percentage*100), "%"))) +
  geom_col(position = position_stack(), color = "black") +
  geom_text(position = position_stack(vjust = .5)) +
  coord_flip() +
  scale_y_continuous(labels = scales::percent_format())

修改

上面的代码适用于更多计划以及更多群体,但情节不会考虑到这一点。只需添加facet_wrap即可生成有关群组的灵活情节:

df %>% 
      filter(Group == "Group1") %>% 
      select(-Total) %>% 
      gather(key = `Plan Type`, value = value, -County, -Group) %>% 
      group_by(County, Group) %>% 
      mutate(Percentage = value/sum(value)) %>% 
      ggplot(aes(x = County, y = Percentage, fill = `Plan Type`, label = paste0(round(Percentage*100), "%"))) +
      geom_col(position = position_stack(), color = "black") +
      geom_text(position = position_stack(vjust = .5)) +
      coord_flip() +
      scale_y_continuous(labels = scales::percent_format()) +
      facet_wrap(~Group)