如何使用 ggMarginal 将边际分布放在左侧?

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

我正在学习用

ggEtra
绘制边际分布。从链接的网站上,我可以将其绘制在右侧,如下所示:

# library
library(ggplot2)
library(ggExtra)
 
# The mtcars dataset is proposed in R
head(mtcars)
 
# classic plot :
p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) +
      geom_point() +
      theme(legend.position="none")
 
# marginal density
p2 <- ggMarginal(p, type="density", margins="y")

,给我下图:

enter image description here

但是,我想知道如何将其绘制在“左侧”。我的目标是这样的: enter image description here

另外,如上所述,我想在主图和边缘图之间放置 y 轴标签。

如果有人建议我如何做到这一点,我将不胜感激。

r ggplot2 histogram density-plot
1个回答
0
投票

在文档中,我没有找到将边际密度图放置在左侧的选项。

但是实现所需结果的一个简单选择是将边缘图创建为单独的图,并使用

patchwork
:

将其添加到主图的左侧
library(ggplot2)
library(patchwork)

p <- ggplot(mtcars, aes(x = wt, y = mpg, color = cyl, size = cyl)) +
  geom_point() +
  theme(legend.position = "none")

p_density <- ggplot(mtcars, aes(y = mpg)) +
  geom_density() +
  theme_void() +
  scale_x_reverse()

p_density + p + 
  plot_layout(widths = c(1, 4))

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