Hands-on Exercise 4: Visualising Distribution

Setup

# Load necessary packages
pacman::p_load(ggdist, ggridges, ggthemes, colorspace, tidyverse)

# Import dataset
exam <- read_csv("/Users/sharon/OneDrive - Singapore Management University/isss608data/hands-on_exercise2/Exam_data.csv")
Rows: 322 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (4): ID, CLASS, GENDER, RACE
dbl (3): ENGLISH, MATHS, SCIENCE

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Ridgeline Plots

Basic Ridgeline Plot using geom_density_ridges()

ggplot(exam, aes(x = ENGLISH, y = CLASS)) +
  geom_density_ridges() +
  theme_minimal()
Picking joint bandwidth of 3.18

Ridgeline Plot with Gradient Fill

ggplot(exam, aes(x = ENGLISH, y = CLASS, fill = stat(x))) +
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
  scale_fill_viridis_c(name = "English Score", option = "C") +
  theme_minimal()
Warning: `stat(x)` was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(x)` instead.
Picking joint bandwidth of 3.18

Ridgeline with ECDF Mapping

ggplot(exam, aes(x = ENGLISH, y = CLASS, fill = stat(ecdf))) +
  stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) +
  scale_fill_viridis_c(name = "ECDF", option = "C") +
  theme_minimal()
Picking joint bandwidth of 3.18

Ridgeline with Quantile Coloring

ggplot(exam, aes(x = ENGLISH, y = CLASS, fill = stat(quantile))) +
  stat_density_ridges(geom = "density_ridges_gradient",
                      quantile_lines = TRUE,
                      quantiles = 4) +
  scale_fill_manual(values = c("#FFEDA0", "#FEB24C", "#F03B20", "#BD0026")) +
  theme_minimal()
Picking joint bandwidth of 3.18

Ridgeline with Custom Quantiles (2.5% and 97.5%)

ggplot(exam, aes(x = ENGLISH, y = CLASS, fill = stat(quantile))) +
  stat_density_ridges(geom = "density_ridges_gradient",
                      quantile_lines = TRUE,
                      quantiles = c(0.025, 0.975)) +
  theme_minimal()
Picking joint bandwidth of 3.18

Raincloud Plots

Half-Eye Plot

ggplot(exam, aes(x = RACE, y = ENGLISH)) +
  stat_halfeye(adjust = 0.5,
               justification = -0.2,
               .width = 0,
               point_colour = NA)

Add Boxplot

ggplot(exam, aes(x = RACE, y = ENGLISH)) +
  stat_halfeye(adjust = 0.5,
               justification = -0.2,
               .width = 0,
               point_colour = NA) +
  geom_boxplot(width = .20, outlier.shape = NA)

Add Dotplot

ggplot(exam, aes(x = RACE, y = ENGLISH)) +
  stat_halfeye(adjust = 0.5,
               justification = -0.2,
               .width = 0,
               point_colour = NA) +
  geom_boxplot(width = .20, outlier.shape = NA) +
  stat_dots(side = "left", dotsize = 1)

Final Raincloud Plot

ggplot(exam, aes(x = RACE, y = ENGLISH)) +
  stat_halfeye(adjust = 0.5,
               justification = -0.2,
               .width = 0,
               point_colour = NA) +
  geom_boxplot(width = .20, outlier.shape = NA) +
  stat_dots(side = "left", dotsize = 1) +
  coord_flip() +
  theme_economist()