【问题标题】:Set margins between plots using multiplot使用多图设置图之间的边距
【发布时间】:2016-04-02 11:41:53
【问题描述】:

为了显示多个图,我使用 multiplot (http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/),现在我有两个图,它们共享相同的 x 轴范围并相互绘制:

multiplot(plot1, plot2)

我使用以下方法删除了 x 轴标签和标题:

xlab(NULL) + theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())

但是两个图之间仍然有一个白边。我怎样才能使这个边距变小或删除它?

【问题讨论】:

  • 如果它们的 x 轴范围相同,为什么不使用 faceting?
  • 由于使用的变量在不同的列中(两种不同类型的计数数据),而不是与 facet wrapping 一起使用的“组”。
  • 但如果值在相同的范围内,我肯定会通过重新排列我的数据框并堆叠这两组来解决这个问题,以便我可以使用分面。
  • 在这种情况下,这不是构建我所追求的图形的最佳方式。
  • 如果问题是数据在两个不同的列中,将可以通过 reshape 轻松调整您的数据。全部完成。

标签: r ggplot2


【解决方案1】:

要减少图之间的空间,请删除顶部图的底部边距并删除底部图的顶部边距。下面的代码将这些边距设置为 0,这仍然会导致图之间有一点空白。您可以使这些边距略微为负(可能是 -0.1 左右)以完全删除空白。我们使用 gridExtra 包中的 grid.arrange 而不是 multiplot 函数来布置绘图。 :

library(grid)
library(gridExtra)

## Create two sample plots with the same x axis using built-in mtcars data frame

# Top plot: Remove bottom margin, x-labels, and x title
p1 = ggplot(mtcars, aes(wt, mpg)) + geom_point() + 
  xlab(NULL) + 
  theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(),
        plot.margin=unit(c(1,1,0,1), "lines"))

# Bottom plot: Remove top margin
p2 = ggplot(mtcars, aes(wt, carb)) + geom_point() +
  theme(plot.margin=unit(c(0,1,1,1), "lines"))

# Lay out plots in one column
grid.arrange(p1, p2, ncol=1) 

上述布局有两个问题:(1) y 轴没有正确对齐,(2) 下图的绘图区域的高度小于上图的绘图区域的高度。下面的代码解决了这些问题:

# Left justify plots
# Source: http://stackoverflow.com/a/13295880/496488
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)

maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)

# Lay out justified plots. Use heights argument to equalize heights of each plot area
grid.arrange(gA, gB, heights=c(0.47,0.53), ncol=1)

您可以使用与左对齐绘图相同的技巧(而不是使用 heightsgrid.arrange 的参数通过肉眼进行操作)精确地平衡每个绘图区域的高度,然后绘图边距加回来。我不确定如何处理,但这里有代码供参考:

maxHeight = grid::unit.pmax(gA$heights[2:5], gB$heights[2:5])
gA$heights[2:5] <- as.list(maxHeight)
gB$heights[2:5] <- as.list(maxHeight)

【讨论】:

  • 我发现这就是我要找的主题(plot.margin=unit(c(0,1,1,1), "lines"))
【解决方案2】:

ggplot2 的最新更新提供了对情节的更多控制。例如:

ggplot(mtcars, aes(disp, mpg)) +
     geom_point() +
     facet_wrap(~vs)

您可以进一步调整标签、行数以及比例的显示方式,例如:nrow = 2scales = "free".

【讨论】:

    【解决方案3】:

    重新排列数据比您想象的要容易,这样您就可以利用 ggplot2 中已经存在的良好对齐功能。请参阅下面的示例复制 eipi10 的答案,但不必使用 ggplotGrob。

    您只需选择要绘制的列以及 ID 列(在本例中为汽车型号和 x 轴值列)。然后融化,并准备好使用标准刻面程序进行绘图。

    请注意,facet_grid 调用中的“切换”选项是一项新功能,您可以通过更新到最新的 CRAN 版本来访问它。我用它来替换常规的 y 轴标题,使用 theme 省略了它。

    这种方法的好处是图总是完美对齐的。

    library("ggplot2")
    library("dplyr")
    library("reshape2")
    
    df <- mtcars %>% 
      add_rownames(var = "Model") %>%
      select(Model, wt, mpg, carb) %>%
      melt(id.vars = c("Model","wt"))
    
    ggplot(df)+
      aes(x=wt, y=value)+
      geom_point()+
      theme(strip.background=element_blank())+
      facet_grid(variable ~ ., scales="free_y", switch="y")
    

    【讨论】:

    • 这对我解决另一个问题很有帮助,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2012-07-05
    • 2019-07-11
    • 1970-01-01
    • 2013-01-30
    相关资源
    最近更新 更多