31

I want to stick two plots without any space between theme (so they share one axis).

Given:

p1 <- qplot(1,1,xlab="")

p1 <- p1 +
  theme(legend.position="none",
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin=unit(c(1,1,0,1), "cm"),
        panel.margin=unit(c(1,1,0,1), "cm"))
p2 <- qplot(1,2)

grid.arrange(p1,p2)

Which produces:

enter image description here

I want to eliminate the white space between the two plots.

I have the impression tweaking heights, as has been done for widths in : left align two graph edges (ggplot) is the solution, but can't figure it out.

Community
  • 1
  • 1
Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87
  • Possible duplicate of [remove the borders from grid.arrange](http://stackoverflow.com/questions/13728272/remove-the-borders-from-grid-arrange) – kdarras Aug 22 '16 at 09:46

2 Answers2

45

You should provide plot.margin for both plots and set negative value for the bottom margin for p1 and upper margin for p2. This will ensure that both plot joins.

p1 <-  qplot(1,1,xlab="")+
  theme(legend.position="none",
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin=unit(c(1,1,-0.5,1), "cm"))
p2 <- qplot(1,2)+
  theme(legend.position="none",
        plot.margin=unit(c(-0.5,1,1,1), "cm"))


grid.arrange(p1,p2)

enter image description here

Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
0

try

+ labs(x=NULL)

OR

+ labs(x=NULL, y=NULL)

to remove the left and bottom margins around the plots (p1, p2) before using grid.arrange

p1 <- qplot(1,1)+
 theme_bw() +
 theme(axis.text.x=element_blank(),
 axis.ticks.x=element_blank(),
 plot.margin = rep(unit(0,"null"),4),
 panel.margin = unit(0,"null"),
 axis.ticks.length = unit(0,"null"),
 axis.ticks.margin = unit(0,"null")) +
 labs(x=NULL)
p2 <- qplot(1,2)+
 theme_bw() +
 theme(
 plot.margin = rep(unit(0,"null"),4),
 panel.margin = unit(0,"null"),
 axis.ticks.length = unit(0,"null"),
 axis.ticks.margin = unit(0,"null"))

grid.arrange(p1,p2)
Gaia
  • 17
  • 3
  • 3
    that's not great either; what if people actually want axis titles? Or legends? Best option would be to properly align the gtables, and there are several posts here that offer more general and reliable solutions – baptiste Jun 17 '13 at 14:01
  • 10
    Mind linking to those better solutions? – Etienne Low-Décarie Jun 17 '13 at 14:27