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")
,给我下图:
另外,如上所述,我想在主图和边缘图之间放置 y 轴标签。
如果有人建议我如何做到这一点,我将不胜感激。
在文档中,我没有找到将边际密度图放置在左侧的选项。
但是实现所需结果的一个简单选择是将边缘图创建为单独的图,并使用
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))