0
votes

I am trying to stack plots with common x- and y-axes in ggplot. What I want to do is have only the bottom plot show the x-axis labels and titles. But I've never been able to figure out how to do this cleanly in ggplot2 without having the bottom plot be squished by carrying the virtue of the x-axis labels/title. There must be an easy way to do this- everyone wants to stack graphs, right?!

I'm currently trying with ggarrange. Example code below. Note that the bottom plot gets compressed vertically because it has the tick and axis labels. I could just have the top two have white font labels/title, but then there is an unseemly amount of margin space between the three if you use that hack.

I'm definitely open to packages other than gpubr, but I am hoping for something not too elaborate that I can use in subsequent situations, as I'm sure I'll encounter this again...

Help, please!! -Ryan

#
require(ggplot2); require(ggpubr)

X=data.frame(seq(as.Date("2001-01-01"),as.Date("2001-12-31"),by='days')); colnames(X)='date'
X$Y1=sample(80:100,size=nrow(X),replace=T)
X$Y2=sample(100:120,size=nrow(X),replace=T)
X$Y3=sample(50:70,size=nrow(X),replace=T)

plot.Y1= ggplot(X, aes(x=date,y=Y1))+
  geom_line()+lims(y=c(50,150))+
  theme(axis.title.x = element_blank(),axis.text.x=element_blank())

plot.Y2= ggplot(X, aes(x=date,y=Y2))+
  geom_line()+lims(y=c(50,150))+
  theme(axis.title.x = element_blank(),axis.text.x=element_blank())

plot.Y3= ggplot(X, aes(x=date,y=Y3))+
  geom_line()+lims(y=c(50,150))

x11(10,8)

ggarrange(plot.Y1,plot.Y2,plot.Y3,nrow=3,ncol=1)

Bottom plot is squished!

1

1 Answers

1
votes

try this,

egg::ggarrange(plot.Y1,plot.Y2,plot.Y3,ncol=1)

enter image description here