如何使用plot_grid放置没有任何空间的地块? [英] How to put plots without any space using plot_grid?

查看:208
本文介绍了如何使用plot_grid放置没有任何空间的地块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在安排2x2地块.这些图共享相同的轴,所以我想将它们放在一起,例如

I'm doing an arrangement of 2x2 plots. The plots share the same axis, so I want to put them together, e.g.

此代码:

library(ggplot2)
library(cowplot)

Value <- seq(0,1000, by = 1000/10)
Index <- 0:10
DF <- data.frame(Index, Value)


plot <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot_grid(plot, plot, plot, plot, align = "hv", ncol = 2)

产生

但是我想要类似的东西:

But I'd like something like:

如何获得相似的结果?

推荐答案

我认为egg包中的ggarrange()函数属于这种情况.用plot_grid()进行此操作需要无休止的摆弄,这是不值得的.

I think this is a case for the ggarrange() function from the egg package. Doing this with plot_grid() would require endless fiddling and isn't worth it.

(技术原因是plot_grid()可以使网格中每个图的总面积保持恒定,但是如果某些图具有x轴,而其他图则没有x轴,则它们占据不同的面积.有人可以尝试绕过此图通过使用rel_heights参数,但是没有很好的方法来计算rel_heights的正确值,因此将是反复试验.相反,ggarrange()分别查看绘图面板和周围的元素,并确保绘图面板具有相同的大小.)

(The technical reason is that plot_grid() keeps the total area for each plot in the grid constant, but if some plots have an x axis and others don’t then they take up different areas. One could try to circumvent this by using the rel_heights argument but there’s no good way to calculate the correct values for rel_heights, so it would be trial and error. By contrast, ggarrange() separately looks at the plot panel and the surrounding elements and makes sure the plot panels have the same size.)

这是使用ggarrange()的代码:

Value <- seq(0,1000, by = 1000/10)
Index <- 0:10
DF <- data.frame(Index, Value)


pbase <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme_bw()

ptopleft <- pbase +
  scale_x_continuous(position = "top") +
  theme(plot.margin = margin(5.5, 0, 0, 5.5),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

ptopright <- pbase +
  scale_y_continuous(position = "right") +
  scale_x_continuous(position = "top") +
  theme(plot.margin = margin(5.5, 5.5, 0, 0),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

pbottomleft <- pbase +
  theme(plot.margin = margin(0, 0, 5.5, 5.5))

pbottomright <- pbase +
  scale_y_continuous(position = "right") +
  theme(plot.margin = margin(0, 5.5, 5.5, 0))

library(egg)      
ggarrange(ptopleft, ptopright,
          pbottomleft, pbottomright,
          ncol = 2)

两条评论:

  1. 要删除顶部图上图面板下方的所有最后一点空间,即使没有显示,我们也需要将x轴移到顶部.这是主题机制的一个奇怪限制.我们不能完全摆脱一个轴.

  1. To remove every last bit of space below the plot panel on the top plots, we need to move the x axis to the top, even though we're not showing it. This is a strange limitation of the theming mechanism. We can't fully get rid of just one axis.

我不喜欢共享轴标题,就像您的示例一样.我认为每个轴都应该有一个标题.如果要共享轴标题,为什么不使用构面机制?

I'm not a big fan of shared axis titles, as in your example. I think each axis should have a title. If you want shared axis titles, why not use the faceting mechanism?

这篇关于如何使用plot_grid放置没有任何空间的地块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆