在 x 轴下方添加箱线图

Adding Box plot below x axis

我有下面的代码,想知道是否有办法让箱形图位于 x 轴或其他包下方。或者将箱线图置于 x 轴下方,背景一般为白色。谢谢!

library(ggstance)
library(ggplot2)

ggplot(diamonds, aes(x = carat, y = -0.5)) +
  
  # horizontal box plot
  geom_boxploth(aes(fill = cut)) +
  
  # normal density plot
  geom_density(aes(x = carat), inherit.aes = FALSE) +
  

  # reproduce original chart's color scale (o/w ordered factors will result
  # in viridis scale by default, using the current version of ggplot2)
  scale_fill_discrete()

您可以使用 patchwork 包:

library(patchwork)

 p <- ggplot(diamonds, aes(x = carat, y = -0.5))
 
 p1 <- p + geom_density(aes(x = carat), inherit.aes = FALSE) 
 p2 <- p + geom_boxploth(aes(fill = cut)) +
       theme_classic() +
       theme(axis.text = element_text(color = 'white'),
             axis.title = element_blank(),
             axis.line = element_blank(),
             axis.ticks = element_blank())
 p1/p2 + plot_layout(ncol = 1, nrow = 2, heights = c(2, 1),
                     guides = 'collect')