将另一个 object 与散点图+边际箱线图对齐

Align another object with scatterplot+marginal boxplots

我使用来自@ClausWilke 的非常有用的(转载如下)生成了 x 轴和 y 轴上带有边缘直方图的散点图。

library(cowplot) 
# Main plot
pmain <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
  geom_point()+
  ggpubr::color_palette("jco")+
  theme(legend.position = c(0.8, 0.8))
# Marginal densities along x axis
xdens <- axis_canvas(pmain, axis = "x")+
  geom_boxplot(data = iris, aes(x = Sepal.Length, fill = Species),
               alpha = 0.7, size = 0.2)+
  ggpubr::fill_palette("jco")
# Marginal densities along y axis
# Need to set coord_flip = TRUE, if you plan to use coord_flip()
ydens <- axis_canvas(pmain, axis = "y", coord_flip = TRUE)+
  geom_boxplot(data = iris, aes(x = Sepal.Width, fill = Species),
               alpha = 0.7, size = 0.2)+
  coord_flip()+
  ggpubr::fill_palette("jco")
p1 <- insert_xaxis_grob(pmain, xdens, grid::unit(.2, "null"), position = "top")
p2<- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
ggdraw(p2) 

效果很好,但我的下一个挑战是将此图用作图中的面板,另一个图 - geom_bar 图 - 位于右侧。我可以使用 plot_grid 将它们并排排列并使 x-axis 很好地对齐,但我希望此 geom_bar 图表的高度仅匹配散点图的高度(pmain) 在上图 p2 中,而不是在 p2.

中达到 x-axis 直方图顶部的高度

其他几件事对我很有帮助...

下面是我的全部尝试...

library(cowplot) 
# Main plot
pmain <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
  geom_point()+
  ggpubr::color_palette("jco")+
  theme(legend.position = c(0.8, 0.8))
# Marginal densities along x axis
xdens <- axis_canvas(pmain, axis = "x")+
  geom_boxplot(data = iris, aes(x = Sepal.Length, fill = Species),
               alpha = 0.7, size = 0.2)+
  ggpubr::fill_palette("jco")
# Marginal densities along y axis
# Need to set coord_flip = TRUE, if you plan to use coord_flip()
ydens <- axis_canvas(pmain, axis = "y", coord_flip = TRUE)+
  geom_boxplot(data = iris, aes(x = Sepal.Width, fill = Species),
               alpha = 0.7, size = 0.2)+
  coord_flip()+
  ggpubr::fill_palette("jco")
p1 <- insert_xaxis_grob(pmain, xdens, grid::unit(.2, "null"), position = "top")
p2<- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
ggdraw(p2) 
# generate a separate bar chart to go alongside
petal.bar <- ggplot(data=iris, aes(y=Petal.Width, x=Species, fill = Species))+
                geom_bar(stat="summary", fun="mean", position = "dodge")+
  theme(axis.text.x = element_blank(),
        legend.position = "none")+
  geom_point()+
  ggpubr::fill_palette("jco")
# place bar chart to the right 
plot_grid(p2, petal.bar, align = "h", axis = "b", nrow = 1, rel_widths = c(1, 0.6))

任何帮助将不胜感激 - 谢谢!

你可以:

  1. element_blank 更改为 axis.text.x = element_text(colour = "white") 以便轴文本与绘图背景颜色相同,因此不可见,强制轴标签位于与主图垂直的位置。
  2. 在对 plot_grid
    的调用中包含一个带有 NULL 的虚拟图 并且,为了回应评论中的其他问题...
  3. 使用 grid::nullGrob 使右侧绘图面板与主绘图面板高度相同。
library(ggplot2)
library(cowplot)
# Main plot
pmain <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
  geom_point()+
  ggpubr::color_palette("jco")+
  theme(legend.position = c(0.8, 0.8))
# Marginal densities along x axis
xdens <- axis_canvas(pmain, axis = "x")+
  geom_boxplot(data = iris, aes(x = Sepal.Length, fill = Species),
               alpha = 0.7, size = 0.2)+
  ggpubr::fill_palette("jco")
# Marginal densities along y axis
# Need to set coord_flip = TRUE, if you plan to use coord_flip()
ydens <- axis_canvas(pmain, axis = "y", coord_flip = TRUE)+
  geom_boxplot(data = iris, aes(x = Sepal.Width, fill = Species),
               alpha = 0.7, size = 0.2)+
  coord_flip()+
  ggpubr::fill_palette("jco")
p1 <- insert_xaxis_grob(pmain, xdens, grid::unit(.2, "null"), position = "top")
p2<- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
# ggdraw(p2) 
# generate a separate bar chart to go alongside
petal.bar <- ggplot(data=iris, aes(y=Petal.Width, x=Species, fill = Species))+
  geom_bar(stat="summary", fun="mean", position = "dodge")+
  theme(axis.text.x = element_text(colour = "white"),
        legend.position = "none")+
  geom_point()+
  ggpubr::fill_palette("jco")

p3 <- insert_xaxis_grob(petal.bar, grid::nullGrob(), grid::unit(.2, "null"), position = "top")
# place bar chart to the right 
plot_grid(p2, NULL, p3, align = "h", axis = "b", nrow = 1, ncol = 3, rel_widths = c(1, 0.1, 0.6))

reprex package (v2.0.1)

于 2022-01-20 创建