Skip to content

Instantly share code, notes, and snippets.

@sammcnerney
Created January 29, 2026 20:40
Show Gist options
  • Select an option

  • Save sammcnerney/064f55a41e42250f80420c3bb89bd164 to your computer and use it in GitHub Desktop.

Select an option

Save sammcnerney/064f55a41e42250f80420c3bb89bd164 to your computer and use it in GitHub Desktop.
# Crosshares-style interface plot (ridgeline distributions)
# Reference script shared alongside the newsletter.
# Not plug-and-play — shared to show structure, not syntax.
#
# Idea:
# 1) Start with wide survey data (one column per item)
# 2) Pivot long
# 3) Clean + clamp responses (0–100)
# 4) Plot distributions per item (ridgeline), optionally ordered by mean
library(tidyverse)
library(ggridges)
library(readxl)
library(janitor)
library(showtext)
library(sysfonts)
# ---- Font setup (optional) ----
# If you don't want fonts, delete this block and set base_family = "" below.
font_add_google("Inter", "Inter")
showtext_auto()
# ------------------------------------------------------------
# 1) INPUT DATA
# ------------------------------------------------------------
# Replace this section with your own data import. Keep the end result:
# a data frame where each item is a column of 0–100-ish values (or text like "not applicable").
# Option A: read from an Excel file (edit the path + sheet)
# df_raw <- read_excel("PATH/TO/YOUR_FILE.xlsx", sheet = 1)
# Option B: a tiny toy dataset to illustrate structure
set.seed(1)
df_raw <- tibble(
`QR code menus` = sample(c(0:100, "not applicable"), 200, replace = TRUE),
`Old couples holding hands` = sample(0:100, 200, replace = TRUE),
`LinkedIn influencers` = sample(0:100, 200, replace = TRUE),
`Clapping when the plane lands` = sample(0:100, 200, replace = TRUE)
) %>%
mutate(respondent_id = row_number())
# ------------------------------------------------------------
# 2) NAME MAPPING (CLEAN NAMES -> ORIGINAL HEADERS)
# ------------------------------------------------------------
# This lets you safely pivot + still display original column labels in the chart.
orig_names <- names(df_raw)
clean_names <- janitor::make_clean_names(orig_names)
name_map <- tibble(
item = clean_names,
item_label_raw = orig_names
)
names(df_raw) <- clean_names
# Identify item columns (everything except the respondent id)
item_cols <- setdiff(names(df_raw), "respondent_id")
# ------------------------------------------------------------
# 3) WIDE -> LONG + CLEANING
# ------------------------------------------------------------
# We treat common NA-ish strings as "not applicable",
# and parse numbers from strings so "85 " or "85%" still works.
na_strings <- c("not applicable", "not appliance", "n/a", "na", "")
long <- df_raw %>%
pivot_longer(
cols = all_of(item_cols),
names_to = "item",
values_to = "value_raw"
) %>%
mutate(
value_raw = str_trim(str_to_lower(as.character(value_raw))),
is_not_applicable = value_raw %in% na_strings,
value = readr::parse_number(value_raw)
) %>%
left_join(name_map, by = "item")
# A light label cleaner: keep it simple on purpose
clean_item_label <- function(x) {
x %>%
as.character() %>%
str_squish() %>%
str_wrap(width = 40)
}
plot_data <- long %>%
filter(!is_not_applicable, !is.na(value)) %>%
mutate(
value = pmin(pmax(value, 0), 100), # clamp to 0–100
item_label = clean_item_label(item_label_raw)
)
# ------------------------------------------------------------
# 4) OPTIONAL: ORDER ITEMS BY MEAN (HIGH -> LOW)
# ------------------------------------------------------------
item_stats <- plot_data %>%
group_by(item_label) %>%
summarize(mean_value = mean(value), .groups = "drop") %>%
arrange(desc(mean_value))
plot_data <- plot_data %>%
left_join(item_stats, by = "item_label") %>%
mutate(item_label = factor(item_label, levels = item_stats$item_label))
# ------------------------------------------------------------
# 5) PLOT
# ------------------------------------------------------------
bandwidth_val <- 4
ridge_scale <- 2.2
min_height <- 0.01
base_size <- 11
y_text_size <- 9
p <- ggplot(plot_data, aes(x = value, y = item_label, fill = mean_value)) +
geom_density_ridges(
scale = ridge_scale,
bandwidth = bandwidth_val,
rel_min_height = min_height,
linewidth = 0.35,
color = "grey25",
alpha = 0.75
) +
# NOTE: Color scale is editorial. Replace or remove.
scale_fill_gradientn(
colors = c("#d73027", "#fee08b", "#1a9850"),
limits = c(0, 100),
guide = "none"
) +
scale_x_continuous(
limits = c(0, 100),
breaks = seq(0, 100, 10),
labels = 0:10,
expand = c(0, 0)
) +
coord_cartesian(clip = "off") +
theme_minimal(base_family = "Inter", base_size = base_size) +
theme(
panel.grid.major.x = element_line(color = "grey90", linewidth = 0.3),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(size = y_text_size, hjust = 0),
axis.ticks.y = element_blank(),
axis.title = element_blank(),
axis.text.x = element_text(size = 9, color = "grey50"),
axis.ticks.x = element_blank(),
plot.margin = margin(t = 6, r = 20, b = 20, l = 10)
)
print(p)
# ------------------------------------------------------------
# 6) EXPORT (OPTIONAL)
# ------------------------------------------------------------
# Keep exports generic (no personal file paths).
# Uncomment and change filenames as needed.
# ggsave("ridgeline.png", p, width = 12, height = 10, dpi = 300, bg = "transparent")
# ggsave("ridgeline.svg", p, width = 12, height = 10, bg = "transparent")
Reference R script for an interface-first survey visualization
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment