有没有办法将来自两个单独堆积条形图的列组合成一个图形?

Mat*_*ndi 1 r bar-chart ggplot2

我想制作一个堆叠条形图,说明按研究类别为每个项目(X 轴)请求和完成了多少研究(Y 轴)。每个项目将有两个堆叠的条形图,一个用于请求的研究,一个用于已完成的研究。

这是我用于数据的示例:

数据

我可以将它分成一组用于要求的研究和一组用于完成的数据,我想出了这个(忽略可笑的颜色和标题):

堆

这是我使用的代码:

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)
Run Code Online (Sandbox Code Playgroud)

有谁知道这是否可能?

All*_*ron 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
Run Code Online (Sandbox Code Playgroud)

我们需要对数据进行整形,使其采用长格式(即所有值都在一个列中,并且有一个新的因子列指定该值是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())
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明