typestar

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

  1. bins or binwidth is a choice you must make explicitly.
  2. A boxplot hides the shape; a violin shows it.
  3. Overlaying points on a boxplot shows the sample size.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 4 in Geometries, step 5 of 15 in ggplot2.

← Previous Next →

Distributions in other languages