'Adding Box plot below x axis
I have the code below and was wondering if there's a way to have the box plot below the the x-axis or another package. Or have the box plot below the x-axis in general with a white background. Thanks!
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()
Solution 1:[1]
You could use the patchwork package:
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')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Allan Cameron |


