-1

I'd like to vertically arrange my stacked geom_bar objects and display them with unbroken vertical lines (see concept below) and a single set of axes and legend. I'm using 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.

my concept:enter image description here

my current plot:enter image description here

  • 4
    Please provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including sample data. Few people will have the time to read through your close to 200 lines R script on GitHub, and you're much more likely to get help if you give us something shorter to work with. – Maurits Evers Dec 18 '17 at 00:26

1 Answers1

2

You could create your plots and disable the axis text, line and ticks. Then make the axis titles match the background color so they are not visible (but retain the same graph dimensions) and plot them with plot_grid() as you are doing. Then overlay a full sized plot with zero data, the axis titles and vertical lines over the top of it using draw_plot(). For the single legend, leverage the following SO answer:

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

The code:

#!/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

The result:

enter image description here

Mike N.
  • 1,662
  • 1
  • 13
  • 19