2

In R basic plot, the outer margins can be controlled through oma(), and I wonder if there is a similar function in ggplot2 to do this. As the following figure shows, the is almost no space between and figure and the edge of the page. Thanks a lot. enter image description here

Please note that I am not asking about set margins between each sub-plot (this can be done by plot.margin= in ggplot2). What I am asking is how to set the outer margin for the whole figure (something like oma in R basic plot). Thanks.

require(cowplot)
require(ggplot2)
x=1:50
y=x^2
z=rep(c("p","sp","n","sn","nt"),each=10)
mydata= data.frame(x,y,z)
plot1=ggplot(mydata,aes(x=x,y=y,group=z))+geom_point(aes(shape=z,color=z),size=1)+scale_shape_manual(values=c(19,1,19,17,17))+scale_color_manual(values=c("blue","black","red","blue","red"))
plot2=ggplot(mydata,aes(x=x,y=y,group=z))+geom_point(aes(shape=z,color=z),size=3)+scale_shape_manual(values=c(19,1,19,17,17))+scale_color_manual(values=c("blue","black","red","blue","red"))
plot3=ggplot(mydata,aes(x=x,y=y,group=z))+geom_point(aes(shape=z,color=z),size=5)+scale_shape_manual(values=c(19,1,19,17,17))+scale_color_manual(values=c("blue","black","red","blue","red"))
plot4=ggplot(mydata,aes(x=x,y=y,group=z))+geom_point(aes(shape=z,color=z),size=7)+scale_shape_manual(values=c(19,1,19,17,17))+scale_color_manual(values=c("blue","black","red","blue","red"))
plot_grid(plot1, plot2, plot1,plot1, align='h', labels=c('a', 'b',"c","d"))
Yang Yang
  • 858
  • 3
  • 26
  • 49
  • 1
    Huh. `plot_grid` returns a ggplot object. go figure. Just add `+ theme(plot.margin=margin(30,30,30,30))` (or whatever space you need) – hrbrmstr Aug 23 '16 at 21:06
  • Thanks a lot. But what I am asking is how to set the outer margin for the whole figure (something like `oma` in R basic plot), not for the sub-plot. – Yang Yang Aug 23 '16 at 21:10
  • it's a bit of hack, but `gridExtra::grid.arrange(plot1, plot2, plot1,plot1,left="",top="",bottom="",right="")` will give you some outer margins ... – Ben Bolker Aug 23 '16 at 21:29

1 Answers1

0

If you wish to stay with cowplot, you could use ggdraw() + draw_plot(), as demonstrated at the bottom of the cowplot introduction. The idea is to set the width and height of each plot to be smaller than they would otherwise be (in this case, 0.4), and position the plots accordingly (e.g. at (0.1, 0.1) rather than (0, 0):

ggdraw() +
  draw_plot(plot1, .1, .5, .4, .4) +
  draw_plot(plot2, .5, .5, .4, .4) +
  draw_plot(plot3, .1, .1, .4, .4) +
  draw_plot(plot4, .5, .1, .4, .4) +
  draw_plot_label(LETTERS[1:4], 
                  x = c(.1, .5, .1, .5), 
                  y = c(.9, .9, .5, .5), size = 15)

enter image description here

Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48