The ggviolinbox is the ggplot2 extention with half violins and boxplots geoms (similar to python matplotlib boxplot/violinplot with side parameter)
You can install the ggviolinbox package from GitHub using the devtools package:
if(!require(devtools)) install.packages("devtools")
devtools::install_github("dsmutin/ggviolinbox")library(ggplot2)
library(ggviolinbox)
ggplot(
mutate(mpg, class = forcats::fct_reorder(class, hwy, .fun = mean)),
aes(class, hwy, fill = class)
) +
geom_halfboxplot(panel = "right", outliers = F, position = position_nudge(x = .2), width = .3) +
geom_jitter(aes(color = class), alpha = .5, position = position_jitter(.1)) +
geom_halfviolin(panel = "left", position = position_nudge(x = -.2), width = .7) +
theme_light()Raincloud plots are not supported directly because of the ggplot2 logic, but you can construct them with geom_violinbox() + geom_jitter() +coord_flip()
The package provides the following functions:
geom_halfviolin(): Creates a half-violin plot (mirrored density plot).geom_halfboxplot(): Creates a half-boxplot (mirrored boxplot).geom_violinboxplot(): Combines a half-violin and a half-boxplot into a single plot.ggviolinbox(): A convenience wrapper forggplot()+geom_violinbox().
In general, all parameters except of panel is inhereted from ggplot2 geom_violin or geom_boxplot
panelcorresponde to the geom side- in
geom_violinbox(),nudgecontrols the width between two geoms
Creates a half-violin plot. Use the panel parameter to specify which side to display.
library(ggplot2)
library(ggviolinbox)
ggplot(mpg, aes(class, hwy)) +
geom_halfviolin(panel = "right") +
theme_minimal()Creates a half-boxplot. Use the panel parameter to specify which side to display.
ggplot(mpg, aes(class, hwy)) +
geom_halfboxplot(panel = "left") +
theme_minimal()Combines a half-violin and a half-boxplot. Use the boxplot and violinplot parameters to specify which side each geom appears on.
ggplot(mpg, aes(class, hwy, fill = class)) +
geom_violinboxplot(boxplot = "left", violinplot = "right",
outliers = F, width = .5) +
theme_minimal()A convenience wrapper for geom_violinbox().
ggviolinbox(boxplot = "left", violinplot = "right") +
theme_minimal()Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. If you'd like to contribute code, fork the repository and submit a pull request.
This package is licensed under the MIT License. See the LICENSE file for details.
- Inspired by the
ggridges,ggExtraandggplot2packages. - Many thanks to the
introindatavizpackage for previous implementations.
For questions or feedback, please contact D. Smutin at dvsmutin@gmail.com.
- Similarly to
ggridgesorggpubr::stat_pwc, only one axis for the categorial variables is now supported. If you want to switch axes, usecoord_flip() - stable on: R 4.4.2, ggplot 4.0.1



