Patchwork 无法识别具有 ggMargnial 分布元素的图
Patchwork doesn't recognize plots with ggMargnial distribution elements
尝试使用 ggMarginal 显示 2 个图,但这些图未被识别。
我将代码的简化版本粘贴到以下位置:
- 在添加 ggMarginal 元素之前 Patchwork 按预期并排显示 ggplots
- 使用 ggMarginal 添加边际分布后,Patchwork 似乎不再识别该图。我收到的错误消息如下:
p1m + p2m
#> Error in p1m + p2m: non-numeric argument to binary operator
这是我的代码 运行:
library(ggplot2)
library(ggExtra)
library(patchwork)
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 1')
p1m <- ggMarginal(p1,
type = "density",
size = 3)
p2 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle('Plot 3')
p2m <- ggMarginal(p2,
type = "density",
size = 3)
p1+p2
p1m + p2m
#> Error in p1m + p2m: non-numeric argument to binary operator
非常感谢您的帮助!
由 reprex package (v2.0.1)
于 2021-10-09 创建
不确定是否有一个简单的选项可以使 patchwork
与 class ggMarginal
的对象一起工作。
添加侧边图的另一种选择是使用 ggside
包,它与 patchwork
配合良好。 ggside
的一个缺点是(据我所知)目前它不提供任何选项来设置副图的样式,即它将继承主图 theme
的样式:
library(ggplot2)
library(ggside)
#> Registered S3 method overwritten by 'ggside':
#> method from
#> +.gg ggplot2
library(patchwork)
p1 <- ggplot(mtcars, aes(mpg, disp)) +
geom_point() +
geom_xsidedensity(aes(y = after_stat(density)), position = "stack") +
geom_ysidedensity(aes(x = after_stat(density)), position = "stack") +
ggtitle('Plot 1') +
theme(ggside.panel.scale = .5)
p2 <- ggplot(mtcars, aes(hp, wt, colour = mpg)) +
geom_point() +
geom_xsidedensity(aes(y = after_stat(density)), position = "stack") +
geom_ysidedensity(aes(x = after_stat(density)), position = "stack") +
ggtitle('Plot 3') +
theme(ggside.panel.scale = .5,)
p1 + p2
第二个但麻烦的选择是也通过 ggplot2
制作边缘图,并使用 patchwork
:
将它们粘合在一起
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 1')
p2 <- ggplot(mtcars) +
geom_point(aes(mpg, disp, colour = mpg)) +
ggtitle('Plot 3')
p1ma <- ggplot(mtcars) +
geom_density(aes(mpg)) +
theme_void()
p1mb <- ggplot(mtcars) +
geom_density(aes(y = disp)) +
theme_void()
wrap_plots(list(p1ma, plot_spacer(), p1ma, plot_spacer(), p1, p1mb, p2, p1mb),
widths = c(2, 1, 2, 1), heights = c(1, 2),
ncol = 4, nrow = 2, byrow = TRUE) &
theme(legend.position = "bottom")
您可以将边际地块包裹在 patchwork::wrap_elements()
中
patchwork::wrap_elements(p1m) + patchwork::wrap_elements(p2m)
尝试使用 ggMarginal 显示 2 个图,但这些图未被识别。
我将代码的简化版本粘贴到以下位置:
- 在添加 ggMarginal 元素之前 Patchwork 按预期并排显示 ggplots
- 使用 ggMarginal 添加边际分布后,Patchwork 似乎不再识别该图。我收到的错误消息如下:
p1m + p2m
#> Error in p1m + p2m: non-numeric argument to binary operator
这是我的代码 运行:
library(ggplot2)
library(ggExtra)
library(patchwork)
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 1')
p1m <- ggMarginal(p1,
type = "density",
size = 3)
p2 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle('Plot 3')
p2m <- ggMarginal(p2,
type = "density",
size = 3)
p1+p2
p1m + p2m
#> Error in p1m + p2m: non-numeric argument to binary operator
非常感谢您的帮助!
由 reprex package (v2.0.1)
于 2021-10-09 创建不确定是否有一个简单的选项可以使 patchwork
与 class ggMarginal
的对象一起工作。
添加侧边图的另一种选择是使用 ggside
包,它与 patchwork
配合良好。 ggside
的一个缺点是(据我所知)目前它不提供任何选项来设置副图的样式,即它将继承主图 theme
的样式:
library(ggplot2)
library(ggside)
#> Registered S3 method overwritten by 'ggside':
#> method from
#> +.gg ggplot2
library(patchwork)
p1 <- ggplot(mtcars, aes(mpg, disp)) +
geom_point() +
geom_xsidedensity(aes(y = after_stat(density)), position = "stack") +
geom_ysidedensity(aes(x = after_stat(density)), position = "stack") +
ggtitle('Plot 1') +
theme(ggside.panel.scale = .5)
p2 <- ggplot(mtcars, aes(hp, wt, colour = mpg)) +
geom_point() +
geom_xsidedensity(aes(y = after_stat(density)), position = "stack") +
geom_ysidedensity(aes(x = after_stat(density)), position = "stack") +
ggtitle('Plot 3') +
theme(ggside.panel.scale = .5,)
p1 + p2
第二个但麻烦的选择是也通过 ggplot2
制作边缘图,并使用 patchwork
:
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 1')
p2 <- ggplot(mtcars) +
geom_point(aes(mpg, disp, colour = mpg)) +
ggtitle('Plot 3')
p1ma <- ggplot(mtcars) +
geom_density(aes(mpg)) +
theme_void()
p1mb <- ggplot(mtcars) +
geom_density(aes(y = disp)) +
theme_void()
wrap_plots(list(p1ma, plot_spacer(), p1ma, plot_spacer(), p1, p1mb, p2, p1mb),
widths = c(2, 1, 2, 1), heights = c(1, 2),
ncol = 4, nrow = 2, byrow = TRUE) &
theme(legend.position = "bottom")
您可以将边际地块包裹在 patchwork::wrap_elements()
patchwork::wrap_elements(p1m) + patchwork::wrap_elements(p2m)