[英]Properly size multiple ggExtraPlot objects after calling ggMarginal in R
I want to create a facet-like effect with ggExtra::ggMarginal
plots.我想用ggExtra::ggMarginal
图创建一个类似刻面的效果。 This is not yet directly possible , so I tried to use patchwork::wrap_plots
, cowplot::plot_grid
, and egg::ggarrange
to do it myself.这还不能直接实现,所以我尝试使用patchwork::wrap_plots
、 cowplot::plot_grid
和egg::ggarrange
自己做。 I have everything properly developed—except for the plot sizing.我把所有东西都做好了——除了情节大小。 patchwork
and egg
make the panels of ggplot
objects the same size, but this behavior does not apply to the ggExtraPlot
objects that ggMarginal
creates. patchwork
和egg
使ggplot
对象的面板大小相同,但此行为不适用于ggMarginal
创建的ggExtraPlot
对象。
For example, consider the following patchwork
of ggplot
s (code below):例如,考虑以下ggplot
的patchwork
(代码如下):
But once I add the marginal densities, the arrangement looks like this:但是一旦我添加了边际密度,排列看起来像这样:
As you can see, the panels of each plot are no longer equally sized;如您所见,每个地块的面板大小不再相等; rather, the entire objects are.相反,整个对象都是。 I want the panel sizes of each ggplot
in the second plot to be the same, as in the first plot.我希望第二个图中每个ggplot
的面板大小与第一个图中相同。
Attempted solutions:尝试的解决方案:
widths
and heights
arguments in wrap_plot
to set proper relative sizes of the ggMarginal
-ized objects;我试图确定轴相对于面板的宽度和高度,因为这样我就可以使用wrap_plot
中的widths
和heights
参数来设置ggMarginal
化对象的适当相对大小; unfortunately, I spent many hours unsuccessfully digging through the ggplotGrob
output.不幸的是,我花了很多时间来挖掘ggplotGrob
输出,但没有成功。 For instance, I used grid::convertWidth(sum(ggplotGrob(p1)$widths), "in", TRUE)
and grid::convertWidth(sum(ggplotGrob(p2)$widths), "in", TRUE)
to attempt to calculate the widths of the plots in inches ( p1
has a y-axis, p2
doesn't), but the results were vastly different: 0.87 and 0.19.例如,我使用grid::convertWidth(sum(ggplotGrob(p1)$widths), "in", TRUE)
和grid::convertWidth(sum(ggplotGrob(p2)$widths), "in", TRUE)
来尝试以英寸为单位计算图的宽度( p1
有 y 轴, p2
没有),但结果大不相同:0.87 和 0.19。 It didn't make sense to me that simply removing the axis title, text, and ticks would so heavily decrease the width.对我来说,简单地删除轴标题、文本和刻度会大大减少宽度,这对我来说没有意义。 Certainly setting the relative widths at around 4.5 and 1 wouldn't generate the proper patchwork
plot!当然,将相对宽度设置为 4.5 和 1 左右不会生成正确的patchwork
图!patchwork::get_dim
and patchwork::set_dim
to manually set the area for each subplot (using patchwork::area
) but couldn't figure it out.我尝试使用patchwork::get_dim
和patchwork::set_dim
手动设置每个子图的区域(使用patchwork::area
)但无法弄清楚。ggMarginal
to each plot in the patchwork, but this didn't work.我还尝试先创建拼凑,然后将ggMarginal
应用于拼凑中的每个情节,但这不起作用。 I would appreciate advice on guaranteeing equal panel sizes for the four plots, ideally without relying on the actual size (ie, in pixels) of the components.我会很感激关于保证四个图的面板大小相等的建议,理想情况下不依赖于组件的实际大小(即,以像素为单位)。 It would be awesome to see ggMarginal
allow faceting and to see patchwork
extend beyond simple ggplot
objects!看到ggMarginal
允许刻面并看到patchwork
超出简单的ggplot
对象,这将是很棒的!
b <- ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point() +
labs(x = "some\nlines",
y = "many\nnew\nlines")
p1 <- b +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
p1m <- ggExtra::ggMarginal(p1, margins = "y")
p2 <- b +
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())
p2m <- ggExtra::ggMarginal(p2, margins = "y")
p3 <- b
p3m <- ggExtra::ggMarginal(p3, margins = "y")
p4 <- b +
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
p4m <- ggExtra::ggMarginal(p4, margins = "y")
patchwork::wrap_plots(p1, p2, p3, p4, nrow = 2) # this looks great!
patchwork::wrap_plots(p1m, p2m, p3m, p4m, nrow = 2) # this doesn't :(
Instead of relying on ggMarginal
one option would be to manually create your density plots and glue both your main and the density plots together using patchwork
:而不是依赖ggMarginal
一种选择是手动创建密度图并使用patchwork
将主图和密度图粘合在一起:
library(ggplot2)
library(patchwork)
dp <- ggplot(mtcars, aes(y = mpg)) +
geom_density() +
theme_void()
list(p1, dp, p2, dp, p3, dp, p4, dp) |>
wrap_plots(nrow = 2, widths = c(5, 1, 5, 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.