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

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

我想垂直排列我的堆叠geom_bar对象,并用不间断的垂直线(见下面的概念)和一组轴和图例显示它们。我现在正在使用plot_grid,但也许应该使用小平面包装?我不确定这是否允许我放置垂直线。生成我当前图的代码是here

我的概念:enter image description here

我目前的情节:enter image description here

r plot ggplot2 facet-wrap
1个回答
2
投票

您可以创建绘图并禁用轴文本,行和刻度。然后使轴标题与背景颜色匹配,使它们不可见(但保留相同的图形尺寸)并使用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

结果:

enter image description here

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