2

I am attempting to make a 2 by 2 plot, where each subplot has y-axis lines allowing the viewer to better see the magnitude of values.

I then arrange them using grid_arrange_shared_legend (requires library(lemon) to work), so that I can use a single legend for this combined plot.

If I look at the individual plots, they have the correct grid lines, specified by theme(panel.grid.major.y=element_line), but once they are plotted together those lines disappear.

Any idea why?

Subplot alone Subplot alone

Combined into 2x2 Combined into 2x2

EDITED to include reproducible example plus images

#making data frame

ASumm <- data.frame("Days" = c("1 Day","2 Days","3 Days","4 Days","5 Days","6 days"), "Condition" = c("C","C","T1","T1","T2","T2"), "mean"= c(20,40,60,80,100,70), "se"=c(3,5,3,8,4,8))

#Specifying graph components
    CondFill<-c("seagreen","darkorange","dodgerblue3") # darker colors from HC code above
    pd<-position_dodge(0.3)
    labels_DaysNum<-c("1","2","3","4","5","6")
    labels_Conditions<-c("Control","Treatment 1","Treatment 2")
    TPShape<-c(3,0,2,1,6,11)

#making subplot
    APlot <- ggplot(ASumm, aes(x=Days, y=mean, colour=Condition, group=Condition)) +
      ggtitle("Subplot") +  
      geom_point(aes(shape=Days),size=6,position=pd,stroke=1.5) +
      scale_shape_manual(values=TPShape) +
      scale_color_manual(values=CondFill,labels=labels_Conditions) +
      labs(y="Title",shape="Time Point") +
      theme_classic(base_size = 40) +
      geom_errorbar(aes(ymin=mean-se, ymax=mean+se), 
                    width=0.3, size=1, position=pd) +
      theme(panel.grid.major.y=element_line(colour="grey", size=1),
            plot.title = element_text(size = 35, hjust = 0.5), 
            axis.text.x = element_text(size=27.5, angle=20), 
            axis.text.y = element_text(size=30), 
            axis.title.x = element_blank(), 
            axis.title.y = element_text(size=35)) +
      scale_y_continuous(breaks=c(0,20,40,60,80,100), 
                         limits=c(0,110), expand=c(0,0))

    #making 2x2 plot with shared legend
    grid_arrange_shared_legend(APlot,APlot,APlot,APlot,nrow=2, ncol = 2, position='right')
tjebo
  • 21,977
  • 7
  • 58
  • 94
Bzrs
  • 121
  • 2
  • 4
    I'm not sure why this is happening and it might be difficult to diagnose without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For now, another option, that also makes plot layouts easier, is the [`patchwork` package](https://github.com/thomasp85/patchwork). If you have four plots and you want a 2x2 grid with a single legend, the layout would be something like this: `{p1 + p2} / {p3 + p4} + plot_layout(guides="collect")` – eipi10 Feb 12 '20 at 17:19
  • Thank you, I just edited to make it reproducible and clearer! – Bzrs Feb 12 '20 at 18:02
  • When I run your code, the gridlines are present in both the individual plot and the 4-plot layout. Can you restart R and try running your code in a clean R session, loading only the ggplot2 and lemon packages? Are the gridlines still missing when you do that? – eipi10 Feb 13 '20 at 01:01
  • Ugh, that's annoying. I just tried a clean run and yes, still get the missing lines. :*( – Bzrs Feb 13 '20 at 21:24
  • 1
    @eipi10 I finally got around to trying this with patchwork and it worked beautifully. Thanks so much for your help! – Bzrs Mar 09 '20 at 19:38

0 Answers0