0

I would like to plot 3 graphics beside each other via the ggplot2 and gridExtra packages. The graphic on the left side has a ylab, the other 2 graphics do not. All three graphics should have the same size and the space between the graphics should be reduced as much as possible. However, due to the ylab of the graphic on the left side, I am either not able to reduce the space as much as I want; or I am cutting off the ylab.

Consider the following example in R:

library("ggplot2")
library("gridExtra")

# Example data
df <- data.frame(x = 1:10,
                 y = 1:10)

# Plots
ggp1 <- ggplot(df, aes(x, y)) + 
  geom_line() + theme_bw() +
  ylab("Here is the ylab")

ggp2 <- ggplot(df, aes(x, y)) + 
  geom_line() + theme_bw() +
  ylab("")

ggp3 <- ggplot(df, aes(x, y)) + 
  geom_line() + theme_bw() +
  ylab("")

# Arrange grids
grid.arrange(ggp1, ggp2, ggp3, ncol = 3)

enter image description here

  • The space between the graphics should be reduced as much as possible.
  • All graphics should have the same size.
  • The ylab of the graphic on the left side should be kept.

I was trying to fix the problem with plot.margin, but unfortunately that didn't work.

Joachim Schork
  • 2,025
  • 3
  • 25
  • 48
  • If you don't mind switching from `gridExtra` to `cowplot`, take a look at `cowplot`'s [vignette](https://cran.r-project.org/web/packages/cowplot/vignettes/plot_annotations.html) on making these sorts of labels – camille Aug 08 '18 at 15:41
  • a couple of quick alts: you can use `egg::ggarrange` to get the same panel size, or you could suppress all of the y-axis titles and then use `left` argument of `grid.arrange`, or use `facets` – user20650 Aug 09 '18 at 00:22
  • so for alternative 2., taking a hint from [here](https://stackoverflow.com/questions/39508304/margins-between-plots-in-grid-arrange) : `plst <- lapply(list(ggp1, ggp2, ggp3), "+", theme(axis.title.y = element_blank(), plot.margin = unit(c(1,0,1,0), "lines"))) ; grid.arrange(grobs = plst, left="my y-axis title", ncol=3)` – user20650 Aug 09 '18 at 00:30
  • Thanks a lot for all the comments! I used the solution of user10199750, since his solution perfectly solves my problem. However, good to know that there are more alternatives. – Joachim Schork Aug 13 '18 at 08:40

2 Answers2

3

I would suggest to cbind() the gtables, with the axis removed. null units automatically ensure equal panel widths.

lg <- lapply(list(ggp1,ggp2,ggp3),ggplotGrob)

rm_axis <- function(g){
  lay <- g[["layout"]]
  cp <- lay[lay$name == "panel",]
  g[,-c(1:(cp$l-1))]
}

lg[-1] <- lapply(lg[-1], rm_axis)

grid::grid.draw(do.call(gtable_cbind, lg))
0

Adding theme (axis.title.y = element_blank()) to ggp2 and ggp3 will reduce the space between them.

library("ggplot2")
library("gridExtra")

# Example data
df <- data.frame(x = 1:10,
                 y = 1:10)

# Plots
ggp1 <- ggplot(df, aes(x, y)) + 
  geom_line() + theme_bw() +
  ylab("Here is the ylab")

ggp2 <- ggplot(df, aes(x, y)) + 
  geom_line() + theme_bw() +
  ylab("") + theme (axis.title.y = element_blank())


ggp3 <- ggplot(df, aes(x, y)) + 
  geom_line() + theme_bw() +
  ylab("") + theme (axis.title.y = element_blank())

# Arrange grids
grid.arrange(ggp1, ggp2, ggp3, ncol = 3)
Lucas Nesi
  • 383
  • 2
  • 13
  • Thanks for your response. Unfortunately, your solution creates plots with different sizes (the plot on the left side is narrower than the other two). – Joachim Schork Aug 08 '18 at 15:29