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()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_violinboxplot().
In general, all parameters except of panel is inhereted from ggplot2 geom_violin or geom_boxplot
panelcorresponds to the geom side ("left"/"right"when groups are on x,"bottom"/"top"when groups are on y)."left"aliases"bottom"and"right"aliases"top"in the flipped orientation.orientationfollows ggplot2 (NAauto-detects, or"x"/"y").- in
geom_violinboxplot(),nudgeshifts the two halves along the grouping axis without replacingposition.
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()
ggplot(mpg, aes(hwy, class)) +
geom_halfviolin(panel = "top") +
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()
ggplot(mpg, aes(hwy, class)) +
geom_halfboxplot(panel = "bottom") +
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()
ggplot(mpg, aes(hwy, class, fill = class)) +
geom_violinboxplot(boxplot = "bottom", violinplot = "top",
outliers = FALSE, width = 0.5) +
theme_minimal()A convenience wrapper for geom_violinboxplot().
ggviolinbox(data = mpg, mapping = aes(class, hwy),
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.
- Requires ggplot2 >= 3.4.0 (orientation /
flip_data()). - Tested with ggplot2 4.0.3 and R 4.3.3.



