有没有一种方法可以将来自两个单独的堆叠条形图的列合并为一个图?

时间:2020-08-24 18:56:04

标签: r ggplot2 bar-chart

我想制作一个堆叠的条形图,该图显示按研究类别为每个项目(X轴)请求并完成的研究(Y轴)数量。每个项目都有两个堆叠的条形,一个用于请求的研究,一个用于完成的研究。

以下是我用于数据的示例:

data

我可以将其分为一组用于请求研究的数据和一组用于完成研究的数据,然后我想到了这一点(忽略可笑的颜色和标题):

stack

这是我使用的代码:

data<-read.csv("table56req.csv")
data
library(ggplot2)
ggplot(data=data, aes(x=proj, y=req, fill=cat)) +
  geom_bar(stat="identity", position="stack") + 
  labs(title="Type of Study by Project", x="Project", y="Number of Studies") + 
  theme(plot.title = element_text(size=30,hjust = 0.5)) + 
  theme(panel.background = element_rect(fill = 'white')) + 
  theme(axis.line = element_line(color="black", size = 0.5)) + 
  theme(axis.text.x= element_text(size=10,color = "black")) + 
  theme(axis.text.y = element_text(size=12, color = "black")) + 
  scale_y_continuous(breaks=seq(0,20,5)) + 
  scale_x_discrete(labels=c("Bowersock","Dorena","Holtwood","Jackson","Milford","Nisqually","Smoky Mtn")) + 
  theme(axis.title.x = element_text(face="bold", size=12,vjust=-0.5,hjust=0.5)) + 
  theme(axis.title.y = element_text(face="bold", size=12,vjust=2,hjust=.5)) + 
  scale_fill_discrete(name = "Categories", labels = c("Biota and Biodiversity","Connectivity and Fragmentation","Geomorphology","Landcover","Water Quality","Water Quantity")) + 
  theme(panel.border = element_rect(colour = "black", fill=NA),legend.box.background = element_rect(colour = "black"), legend.background = element_rect(linetype = "solid", colour = "black")) + 
  theme(legend.title.align=0.5)

有人知道这是否有可能吗?

1 个答案:

答案 0 :(得分:5)

是的,有可能,但是您可能需要使用构面。这不是一个大问题,因为刻面很容易隐藏。

您的问题带有数据图像,而不是可复制和粘贴的任何图像,因此我制作了一个简单的可复制示例,其结构与您的数据框类似:

df <- data.frame(cat = factor(paste("cat ", rep(letters[1:5], each = 4))),
                 proj = factor(paste("proj", rep(LETTERS[1:4], 5))),
                 req = c(0, 10, 10, 11, 7, 11, 11, 1, 2, 3,
                         2, 2, 1, 0, 0, 2, 0, 1, 2, 1),
                 comp = c(0, 12, 11, 15, 6, 15, 15, 1, 2, 3, 
                         2, 1, 1, 1, 0, 1, 2, 1, 2, 1))
df
#>       cat   proj req comp
#> 1  cat  a proj A   0    0
#> 2  cat  a proj B  10   12
#> 3  cat  a proj C  10   11
#> 4  cat  a proj D  11   15
#> 5  cat  b proj A   7    6
#> 6  cat  b proj B  11   15
#> 7  cat  b proj C  11   15
#> 8  cat  b proj D   1    1
#> 9  cat  c proj A   2    2
#> 10 cat  c proj B   3    3
#> 11 cat  c proj C   2    2
#> 12 cat  c proj D   2    1
#> 13 cat  d proj A   1    1
#> 14 cat  d proj B   0    1
#> 15 cat  d proj C   0    0
#> 16 cat  d proj D   2    1
#> 17 cat  e proj A   0    2
#> 18 cat  e proj B   1    1
#> 19 cat  e proj C   2    2
#> 20 cat  e proj D   1    1

我们需要调整数据的形状,使其采用长格式(即所有值都在一列中,并且有一列新的因子指定值是req还是comp )。您可以为此使用tidyr::pivot_longer

pivot_longer(df, cols = c("req", "comp")) %>%
  ggplot(aes(name, value, fill = cat)) +
  geom_col() +
  facet_grid(~proj, switch = "x") +
  scale_x_discrete(expand = c(0.5, 0.5)) +
  scale_fill_viridis_d() +
  labs(x = "Project") + 
  theme_classic() +
  theme(panel.spacing = unit(0, "points"),
        strip.placement = "outside",
        strip.background = element_blank())

enter image description here