11
votes

I am trying to combine n number of barplots with one common label plot at the bottom. My problem is that grid.arrange combines the two plots in 50%-50%. I am looking for something like the layout matrix where you can specify 4 slots and first 3 to be taken up by first plot and last slot by second plot. And customise similarly depending on number of plots.

Result of code below

I have a sample code here from what I am trying:

#load libraries
require(ggplot2)
require(reshape)
require(grid)
require(gridExtra)

#data creation
#DATA
temp<-data.frame(var1=sample(0:100,100,replace=T))
temp$var2<-100-temp$var1
temp$type<-factor(c(rep("S1",50),rep("S2",50)))
temp$label<-factor(rep(1:50,2))
temp1<-melt(temp,id.var=c("type","label"))
#LABELS
labs1<-data.frame(pos=c(1,8,22,45,50))
labs2<-data.frame(pos1=round((diff(labs1$pos)/2)+labs1$pos[1:length(labs1$pos)-1],1),
         lab=c("A","B","D","E"))

#plots
plot1<-ggplot(data=temp1)+
geom_bar(aes(x=label,y=value,fill=variable),stat="identity",space=0,width=1)+
facet_grid(type~.)+theme_bw()+labs(x=NULL,y=NULL)+
scale_y_continuous(expand=c(0,0))+
theme(legend.position="none",axis.text=element_blank(),axis.ticks=element_blank())
plot2<-ggplot()+
geom_line(data=labs1,aes(x=pos,y=-0.05),size=0.6)+
geom_point(data=labs1,aes(x=pos,y=-0.05))+labs(x=NULL,y=NULL)+
geom_text(data=labs2,aes(x=pos1,y=-0.1,label=lab))+
theme_bw()+scale_x_continuous(expand=c(0,0))+scale_y_continuous(limit=c(-0.5,0))+
theme(legend.position="none",axis.text=element_blank(),axis.ticks=element_blank())

plot3<-grid.arrange(plot1, plot2)
#here perhaps there is a way to say plot1 to take up 1/3 of the plot area.

The two plots don't align quite perfectly. But that's another issue. And this was useful How can I arrange an arbitrary number of ggplots using grid.arrange? I could plot each one by one, but I would like to have the facet labels.

2
Your code doesn't run. In the statement labs2<- data.frame(pos1<- round..., there's no lab$pos. Plus I think you want pos1= round(... not pos1<- round(...jlhoward
works if you change labs by labs1 and add gridExtra ..agstudy
sorry about that. Its fixed now.rmf

2 Answers

23
votes
grid.arrange(plot1, plot2, widths=c(0.7, 0.3), ncol=2)

sorry for your example:

grid.arrange(plot1, plot2, heights=c(0.7, 0.3), nrow=2)

EDIT - for spacing between plots:

blank<-rectGrob(gp=gpar(col="white")) # make a white spacer grob
grid.arrange(plot1, blank, plot2, heights=c(0.7, 0.05, 0.25), nrow=3)
9
votes

grid.arrange() won't let you align the panels properly, it's better to work with gtable.

require(gtable)

g1 <- ggplotGrob(plot1)
g2 <- ggplotGrob(plot2)
## add dummy column for missing strips
g2 <- gtable_add_cols(g2, unit(0,"mm"))
## merge, basing widths on g1
g <- gtable:::rbind_gtable(g1, g2, "first")
## add spacing
g <- gtable_add_rows(g, unit(1,"cm"), pos=nrow(g1))
grid.newpage()
grid.draw(g)

enter image description here

Note that the panel heights are equal by default with this strategy. That's because they appear as unit(1, "null") in the layout (g$heights). It's trivial to edit g$heights if another ratio is required.