如何使用 ggplot2 创建带有总计列和所有条形百分比标签的堆积条形图?

问题描述 投票:0回答:1

我正在尝试生成一个堆积条形图,其中每个条形中有一列总计和百分比标签。但不知怎的,我只能做到其中之一。任何帮助同时实现这两件事的帮助将不胜感激。

这是我的 farm_size1 数据库的摘录:

structure(list(SPTOT = c(2L, 6L, 5L, 5L, 4L, 4L, 4L, 3L, 2L, 
2L, 2L, 3L, 4L, 2L, 5L, 2L, 3L, 4L, 4L, 4L), CASE = c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), case1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("1", 
"2", "3", "4"), class = "factor"), case2 = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), levels = c("Bierge", "Santa Creu", "Collbató", "Monells"
), class = "factor"), sptot1 = structure(c(2L, 6L, 5L, 5L, 4L, 
4L, 4L, 3L, 2L, 2L, 2L, 3L, 4L, 2L, 5L, 2L, 3L, 4L, 4L, 4L), levels = c("1", 
"2", "3", "4", "5", "6"), class = "factor"), sptot2 = c("50-100 Ha", 
"> 500 Ha", "> 500 Ha", "> 500 Ha", "201-500 Ha", "201-500 Ha", 
"201-500 Ha", "101-200 Ha", "50-100 Ha", "50-100 Ha", "50-100 Ha", 
"101-200 Ha", "201-500 Ha", "50-100 Ha", "> 500 Ha", "50-100 Ha", 
"101-200 Ha", "201-500 Ha", "201-500 Ha", "201-500 Ha")), row.names = c(NA, 
20L), class = "data.frame")

这是我用来生成带有总计列的比例堆积条形图的代码:

farm_size1 %>%
  ggplot(aes(x=case2)) +
  geom_bar(aes(fill=sptot2), position="fill")+
  geom_bar(aes(x="Total", fill=sptot2), position= "fill")+
  scale_y_continuous(expand = c(0,0), labels = c("0", "25", "50", "75", "100"))+
  scale_fill_manual(values=c("#FB8072","#FBB4AE","#FDB462", "#2a7dad","#8DD3C7","#E6F5C9"),
                    labels=c("< 50 Ha", "50-100 Ha", "101-200 Ha", "201-500 Ha", "> 500 Ha"))+
  labs(x= "", y="Percentage", fill= "Farm size")

看起来像这样:

stacked bar chart with totals' column

生成百分比标签的代码是这样的:

farm_size1 %>%
  count(case2, sptot2)%>%
  group_by(case2)%>%
  mutate(pct=prop.table(n)*100)%>%
  ggplot()+
  aes(case2, pct, fill=sptot2)+
  geom_bar(stat = "identity")+
  ylab("Percentage of farms") +
  geom_text(aes(label=paste0(sprintf("%1.1f", pct),"%")),
            position=position_stack(vjust=0.5), size=3)+
  scale_fill_manual(values=c("#FB8072","#FBB4AE","#FDB462", "#2a7dad","#8DD3C7","#E6F5C9"),
                    labels=c("< 50 Ha", "50-100 Ha", "101-200 Ha", "201-500 Ha", "> 500 Ha"))

我制作了下图: stacked bar chart with labels

r ggplot2 bar-chart
1个回答
0
投票

您可以将新数据添加到数据框中,以便每个

case2
都相等“总计”

farm_size1 %>%
  #new code
  bind_rows(farm_size1 %>% mutate(case2 = "Total")) %>% 
  #########
  count(case2, sptot2)%>%
  group_by(case2)%>%
  mutate(pct=prop.table(n)*100)%>%
  ggplot()+
  aes(case2, pct, fill=sptot2)+
  geom_bar(stat = "identity")+
  ylab("Percentage of farms") +
  geom_text(aes(label=paste0(sprintf("%1.1f", pct),"%")),
            position=position_stack(vjust=0.5), size=3)+
  scale_fill_manual(values=c("#FB8072","#FBB4AE","#FDB462", "#2a7dad","#8DD3C7","#E6F5C9"),
                    labels=c("< 50 Ha", "50-100 Ha", "101-200 Ha", "201-500 Ha", "> 500 Ha"))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.