Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing all the space between two ggplots combined with grid.arrange

Tags:

r

ggplot2

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.

like image 704
Etienne Low-Décarie Avatar asked Mar 21 '13 19:03

Etienne Low-Décarie


People also ask

How do I arrange multiple Ggplots?

To arrange multiple ggplot2 graphs on the same page, the standard R functions - par() and layout() - cannot be used. The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page.

How do you reduce space between plots in Ggarrange?

Currently, the only way to change the space between multiple plot is ggarrange() is to change the margins of each plot (#58). An option to change the spacing in the ggarrange() itself would improve the user experience.

What does Grid Arrange do in R?

grid. arrange() makes no attempt at aligning the plot panels; instead, it merely places the objects into a rectangular grid, where they fit each cell according to the varying size of plot elements. The following figure illustrates the typical structure of ggplots.


1 Answers

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

like image 136
Didzis Elferts Avatar answered Sep 30 '22 07:09

Didzis Elferts