Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Forked from jbburant/half_violin_with_raw_data.R
Last active April 14, 2024 22:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmarwick/b7dc863d53e0eabc272f4aad909773d2 to your computer and use it in GitHub Desktop.
Save benmarwick/b7dc863d53e0eabc272f4aad909773d2 to your computer and use it in GitHub Desktop.
## GOAL:
## re-create a figure similar to Fig. 2 in Wilson et al. (2018),
## Nature 554: 183-188. Available from:
## https://www.nature.com/articles/nature25479#s1
##
## combines a boxplot (or violin) with the raw data, by splitting each
## category location in two (box on left, raw data on right)
## call required packages
library("tidyverse")
## load source code
source("geom_flat_violin.R")
## sourced from github "dgrtwo/geom_flat_violin.R (actually from my fork, which has a correction)
devtools::source_gist("2a1bb0133ff568cbe28d", filename = "geom_flat_violin.R")
# half violin plot with raw data ------------------------------------------
## create a violin plot of Sepal.Length per species
## using the custom function geom_flat_violin()
ggplot(data = iris,
mapping = aes(x = Species,
y = Sepal.Length,
fill = Species)) +
geom_flat_violin(scale = "count",
trim = FALSE) +
stat_summary(fun.data = mean_sdl,
fun.args = list(mult = 1),
geom = "pointrange",
position = position_nudge(0.05)) +
geom_dotplot(binaxis = "y",
dotsize = 1,
stackdir = "down",
binwidth = 0.1,
position = position_nudge(-0.025)) +
theme(legend.position = "none") +
labs(x = "Species",
y = "Sepal length (cm)") +
theme_minimal()
@benmarwick
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment