1

I have four ggplot2 objects to be arranged in a 2-by-2 layout figure, and I refer to this function here: the multiplot function works well if each of my four plots is provided. For example:

multiplot(p1, p2, p3, p4, cols=2, layout=matrix(seq(1,4), nr=2, byrow=TRUE))

However, in each of the plot, I suppress the x- and y-axis labels, because they are identical and I hope to put a global x-axis label and a global y-axis label on the 2-by-2 big panel. Is there a way to modify the multiplot function to achieve this?

There is a related post here, but it seems to be outdated. It also provides a reproducible example. Thank you!

Update

Thanks Scott for the solution. One more question while I add the axis labels: how can I control the margin of the entire figure? I tried to use plot.margin in theme, but the problem of not showing the complete labels still remain. Please see the attached screenshot below:

enter image description here

Community
  • 1
  • 1
alittleboy
  • 10,616
  • 23
  • 67
  • 107

1 Answers1

2

You can use grid.text to add labels wherever you want them by passing into the multiplot function. For example:

https://gist.github.com/sckott/8444444

And you could easily add in a parameter to multiplot to pass in placement of the labels.

Sorry, there's a lot of code, so it's all in the gist instead...

sckott
  • 5,755
  • 2
  • 26
  • 42
  • thank you so much! one more question about margins. Please see my update in OP :) – alittleboy Jan 15 '14 at 21:25
  • That should just be the placement of the labels via `grid.text`. Try the updated function in my gist. I added params for x and y positions of each label – sckott Jan 15 '14 at 22:19