简体   繁体   English

在不同的axis.title/text属性的情况下调整面板图(ggplot)子图之间的间距

[英]Adjust spacing between subplots of panel figure (ggplot) in case of different axis.title/text properties

I would like to adjust the spacing between plots that are aligned in a panel using the cowplot package when some plots contain axis titles/labels, and others don't.当某些图包含轴标题/标签而其他图不包含时,我想使用cowplot package 调整面板中对齐的图之间的间距。

Example例子

Let's create three plots:让我们创建三个图:

library(tidyverse)
library(cowplot)

set.seed(123)

df <- data.frame(x = rnorm(n = 100), 
                 y = rnorm(n = 100))

plot <- ggplot(data = df, aes(x, y)) + geom_point()

plot_grid(plot, plot, plot, nrow = 1, align = "vh") 

在此处输入图像描述

These plots are aligned perfectly, But often.这些情节完美地对齐,但经常。 I have a scenario in which I would like to create a 'cleaner' panel figure.我有一个场景,我想创建一个“更干净”的面板图。 One way to do this is to remove the titles/text of the y-axis of the second and third plots.一种方法是删除第二个和第三个图的 y 轴的标题/文本。

Like this:像这样:

plot2 <- plot + theme(axis.title.y = element_blank(),  
                      axis.text.y = element_blank())

plot_grid(plot, plot2, plot2, nrow = 1, align = "vh")

在此处输入图像描述

Again, perfectly aligned, but the spacing between the first and the second plot (and the second and third plot) is quite large.同样,完美对齐,但第一个和第二个 plot(以及第二个和第三个图)之间的间距非常大。 I would like to reduce the spacing to create a more compact plot, while the axis remain exactly the same size.我想减少间距以创建更紧凑的 plot,同时轴保持完全相同的尺寸。

Expected output预期 output

在此处输入图像描述

Is this possible with cowplot ?这可能与cowplot吗? Or is there another way to do this?还是有其他方法可以做到这一点?

Referencing this post on github , plot_grid() doesn't add any space by default and uses the margins of your plot.参考github 上的这篇文章plot_grid()默认不添加任何空格,并使用 plot 的边距。 To remove the space outside your plot area, you can use them(plot.margin=...) to remove.要删除 plot 区域之外的空间,可以使用them(plot.margin=...)来删除。

With that being said... that's not what's going on here!话虽如此......这不是这里发生的事情! Printing either plot or plot2 will yield a plot with no margins.打印plotplot2将产生没有边距的 plot。 It appears the issue is with the use of the align= argument in plot_grid() .看来问题在于在plot_grid()中使用了align=参数。 I'm not sure why, but setting it to anything other than the default values ( align="none" ) results in the extra whitespace around the plots.我不知道为什么,但是将其设置为默认值( align="none" )以外的任何值都会导致绘图周围出现额外的空白。 Very strange... needless to say, removing that argument fixes your problem:很奇怪...不用说,删除该论点可以解决您的问题:

Original code using align="vh"使用 align="vh" 的原始代码

plot_grid(plot, plot2, plot2, nrow = 1, align="vh")

在此处输入图像描述

Using align="none"使用 align="none"

plot_grid(plot, plot2, plot2, nrow = 1, align="none")

在此处输入图像描述

Any further space would be added according to your graphics device, since the actual plot you get depends on the size and resolution of that device.将根据您的图形设备添加任何更多空间,因为您获得的实际 plot 取决于该设备的大小和分辨率。

Here is a solution using the patchwork package这是使用patchwork的 package 的解决方案

library(tidyverse)

set.seed(123)

df <- data.frame(x = rnorm(n = 100), 
                 y = rnorm(n = 100))

plot1 <- ggplot(data = df, aes(x, y)) + geom_point()

plot2 <- plot1 + theme(axis.title.y = element_blank(),  
                      axis.text.y = element_blank())

# install.packages("patchwork", dependencies = TRUE)
library(patchwork)

plot1 + plot2 + plot2 +
  plot_layout(ncol = 3) 

Created on 2020-07-24 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 7 月 24 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM