如何使用单组轴和图例垂直排列 ggplots?

How to vertically arrange ggplots with single set of axes and legend?

我想垂直排列堆叠的 geom_bar 对象并用完整的垂直线(参见下面的概念)和一组轴和图例显示它们。我正在使用 plot_grid now but should perhaps be using facet wrapping? I'm unsure whether that would allow me to place vertical lines. The code that generates my current plot is here.

我的概念:

我目前的剧情:

您可以创建绘图并禁用轴文本、线条和刻度。然后使轴标题与背景颜色匹配,使它们不可见(但保留相同的图形尺寸)并像您一样用 plot_grid() 绘制它们。然后使用 draw_plot() 在其顶部覆盖一个具有零数据、轴标题和垂直线的全尺寸图。对于单个图例,利用以下 SO 答案:

Align multiple plots in ggplot2 when some have legends and others don't

代码:

#!/usr/bin/env Rscript                                                                                                                                                                                      
if (!require("pacman")) install.packages("pacman")
pacman::p_load(ggplot2, cowplot)

### Create some garbage data to plot                                                                                                                                                                        

d0 <- data.frame(foo=c(0,0,0,0,0),bar=c("SX_RUNNYNOSE","SX_COUGH","SX_HEADACHE","SX_MALAISE","SX_MYALGIA"))
d1 <- data.frame(foo=c(1,2,3,4,5),bar=c("SX_RUNNYNOSE","SX_COUGH","SX_HEADACHE","SX_MALAISE","SX_MYALGIA"))

### Create a plot with 0 data but having the axis titles and vertical lines                                                                                                                                 

p0 <- ggplot(d0, aes(x=seq(1,5), y=foo, fill=bar)) +
    geom_bar(stat="identity") +
    theme(axis.text.x=element_blank(),
          axis.text.y=element_blank(),
          axis.line.x=element_blank(),
          axis.line.y=element_blank(),
          axis.ticks.x=element_blank(),
          axis.ticks.y=element_blank()
          ) +
    theme(legend.position = "none") +
    geom_segment(aes(x=2, y=0, xend=2, yend=4.9), color='red') +
    geom_text(aes(x=2, y=max(d1$foo), label="T0")) +
    geom_segment(aes(x=3, y=0, xend=3, yend=4.9), color='red') +
    geom_text(aes(x=3, y=max(d1$foo), label="T24")) +
    labs(y="Continued Symptom Count Among Samples", x="Time Elapsed Since Viral Challenge")

### A bar pot with the sample data and only the bars (no axis, etc)                                                                                                                                         
### Make color of axis titles white to match the background color so they are not visible                                                                                                                   

p1 <- ggplot(d1, aes(x=seq(1,5), y=foo, fill=bar)) +
    geom_bar(stat="identity") +
    theme(axis.text.x=element_blank(),
          axis.text.y=element_blank(),
          axis.line.x=element_blank(),
          axis.line.y=element_blank(),
          axis.ticks.x=element_blank(),
          axis.ticks.y=element_blank(),
          axis.title.x = element_text(colour = "white"),
          axis.title.y = element_text(colour = "white")
          ) +
    theme(legend.title=element_blank())

### Arrange bar plots and legends in a grid and use draw_plot to                                                                                                                                            
### overlay the single axis titles and vertical bars across all                                                                                                                                             
### plots                                                                                                                                                                                                   

g <- plot_grid(
plot_grid(
        p1 + theme(legend.position = "none")
      , p1 + theme(legend.position = "none")
      , p1 + theme(legend.position = "none")
      , ncol = 1
      , align = "h"
      , labels=c("Rhinovirus", "H3N2", "H1N1")
      , hjust=c(-0.5,-1,-1)) +
draw_plot(p0, 0, 0, 1, 1, 1)
  , plot_grid(
        ggplot()
      , get_legend(p1)
      , ggplot()
      , ncol =1)
  , rel_widths = c(9,3)
)

g

结果: