Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R setting space between graphs on a multiplot

Following a previous R post Specifying ggplot2 panel width, I have been able to produce this plot:

enter image description here

with this code.

You can find the output of dput(datos) at http://ubuntuone.com/0Nlb97mOeDhSbFrbFKCeEG

Now my question is how can I remove/reduce the white space between the graphs. I have found examples with ggExtra package, ggplot and facet, multiplots with options as plot.margin or panel.margin but couldn't find how to apply to my case.

Thanks for your help.

EDIT: I have just noticed that plots have not the same width. It is needed they have the same width so they can share x axis labels from bottom plot.

like image 509
pacomet Avatar asked Oct 26 '11 11:10

pacomet


2 Answers

Using xlab(NULL) instead of xlab(" ") will remove some space off the bottom of each plot.

Using opts(plot.margin = unit(c(0,0,0,0), "cm")) will remove a little space from the edges.


I think that your have overcomplicated things by creating 5 separate graphs and recombining them. Faceting is much easier.

mdatos <- melt(datos[, -1], id.vars = "dia")
(p_all <- ggplot(mdatos, aes(dia, value)) +
  geom_line(colour = "blue") +
  facet_grid(variable ~ ., scale = "free_y") +
  xlab("Day") +
  ylab(NULL) 
)

The plot panels aren't the same width because some y axis labels have three digit numbers, and some only two. Either change the formatting of the y axis, or use my facetting suggestion.

like image 53
Richie Cotton Avatar answered Oct 04 '22 03:10

Richie Cotton


You can layout and control the margins of several subplots with par and layout. For example:

  par (fig=c(0,1,0,1), # Figure region in the device display region (x1,x2,y1,y2)
       omi=c(0,0,0.3,0), # global margins in inches (bottom, left, top, right)
       mai=c(0.1,0.1,0.3,0.1)) # subplot margins in inches (bottom, left, top, right)
  layout(matrix(1:4, 2, 2, byrow = TRUE))
like image 38
Itamar Avatar answered Oct 04 '22 04:10

Itamar