使用多图但仅使用一个图

时间:2018-08-02 14:07:50

标签: r ggplot2 graph

可能的问题有点奇怪。我会向您解释我的情况,也许有人知道另一种解决方法。假设我有5个图(为方便起见,假设图3和图5相同)

library(ggplot2)

# This example uses the ChickWeight dataset, which comes with ggplot2
# First plot
p1 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
  geom_line() 

# Second plot
p2 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
  geom_point(alpha=.3) +
  geom_smooth(alpha=.2, size=1) 


# Third plot
p3 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
  geom_density()

# Fourth plot
p4 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
  geom_histogram(colour="black", binwidth=50) +
  facet_grid(Diet ~ .) +
  theme(legend.position="none")   # No legend (redundant in this graph)   

p5 <- p3

multiplot(p1, p2, p3, p4, cols=2)

我正在使用多图功能创建一张带有4个子图的图片。我想知道用2列创建第二个“ multiplot”,但仅在图形上。但是我没有找到解决方案。 背景是这4个地块占据了Din-A4一侧。而且我不想减小它们的大小以创建具有5个子图的多图。因此,我想将4个图和一个图分成两个图形,但是第5个图的大小应与4个子图的大小相同。因此,我的想法是创建另一个具有2个列但一个空图的多图。希望你能解决我的问题

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您不强烈依赖egg::ggarrange(),则可以使用具有定义宽度的png()。为了获得有关宽度的最佳结果,请用p.empty <- ggplot() + theme_void() png("plot1.png", width = 480, height = 480) egg::ggarrange(p1, p2, p3, p4, ncol=2, widths=c(3, 3)) dev.off() png("plot2.png", width = 480, height = 480) egg::ggarrange(p5, p.empty, p.empty, ncol=2, nrow=2, widths=c(3, 5)) dev.off() 。但是,单个图的高度有些棘手。为正确起见,您可以添加一个完全空白的图。

widths=c(3, 5)

注意:参数{{1}}的第二部分较大,因为在空白图表中缺少图例。

结果

第一个情节

enter image description here

第二个情节

enter image description here