R setting space between graphs on a multiplot

谁都会走 提交于 2019-12-22 03:45:20

问题


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

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.


回答1:


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.




回答2:


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))


来源:https://stackoverflow.com/questions/7901925/r-setting-space-between-graphs-on-a-multiplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!