如何使用拼凑组合多个 ggplot2 图?

问题描述 投票:0回答:1

当尝试使用

+
中的
patchwork
添加两个 ggplot 对象(图)时,我收到如下所示的错误。可能是什么问题?

require(patchwork)
ex.df <- data.table(
  x1 = rnorm(1000, 20, 50),
  x2 = rnorm(1000, 4, 5)
)

plt1 <- ex.df |> 
  ggplot() +
  geom_density(aes(x = x1))

plt2 <- ex.df |> 
  ggplot() +
  geom_density(aes(x = x2))
plt1 + plt2

即使我使用

wrap_plots
功能也会发生这种情况

r ggplot2 rstudio patchwork
1个回答
0
投票

data.frame

library(ggplot2)
require(patchwork)

ex.df <- data.frame(
  x1 = rnorm(1000, 20, 50),
  x2 = rnorm(1000, 4, 5)
)

plt1 <- ex.df |> 
  ggplot() +
  geom_density(aes(x = x1))

plt2 <- ex.df |> 
  ggplot() +
  geom_density(aes(x = x2))

plt1 + plt2

创建于 2024-03-17,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.