Distributions in R
Histogram, density, boxplot and violin, each answering a different question.
library(ggplot2)
library(tibble)
set.seed(1)
runs <- tibble(
lang = rep(c("r", "python"), each = 60),
tpm = c(rnorm(60, 92, 8), rnorm(60, 101, 10))
)
histogram <- ggplot(runs, aes(tpm)) +
geom_histogram(binwidth = 5, fill = "gray70", colour = "white")
shapes <- ggplot(runs, aes(lang, tpm)) +
geom_violin(fill = "gray90") +
geom_boxplot(width = 0.15, outlier.shape = NA) +
geom_jitter(width = 0.08, alpha = 0.4, size = 0.8)
print(length(shapes$layers))
ggsave(tempfile(fileext = ".png"), histogram, width = 4, height = 3, dpi = 100)
How it works
binsorbinwidthis a choice you must make explicitly.- A boxplot hides the shape; a violin shows it.
- Overlaying points on a boxplot shows the sample size.
Keywords and builtins used here
aescgeom_boxplotgeom_histogramgeom_jittergeom_violinggplotggsavelengthlibraryprintreprnormtempfiletibble
The run, in numbers
- Lines
- 19
- Characters to type
- 536
- Tokens
- 165
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 99 seconds.
Step 2 of 4 in Geometries, step 5 of 15 in ggplot2.