# Helpers for the pain-perception report: KDS-inspired palette/theme and body-map geometry. suppressPackageStartupMessages({library(tidyverse); library(scales)}) kds <- list(primary = "#2a69c5", primary_dark = "#1652a6", primary_bg = "#eef6ff", primary_border = "#97c4fc", text = "#121212", muted = "#555555", border = "#e1e1e1", surface = "#f5f5f5", danger = "#d60929", danger_bg = "#fff2f1", success = "#067c30", warning = "#e0a100") theme_kela <- function(base_size = 17) { theme_minimal(base_size = base_size, base_family = "Lato") + theme(text = element_text(colour = kds$text), plot.title = element_text(face = "bold", size = rel(1.1)), plot.title.position = "plot", panel.grid.minor = element_blank(), panel.grid.major.y = element_blank(), panel.grid.major.x = element_line(colour = kds$border), axis.title = element_text(colour = kds$muted), axis.text = element_text(colour = kds$text), legend.position = "top", legend.justification = "left", strip.text = element_text(face = "bold", hjust = 0), plot.margin = margin(4, 8, 4, 4)) } # ---- body map geometry (same shapes as the survey widget; y grows downwards) ------------------ rrect <- function(x, y, w, h, r = 0, n = 10) { r <- min(r, w / 2, h / 2) if (r == 0) return(tibble(x = c(x, x + w, x + w, x), y = c(y, y, y + h, y + h))) corner <- function(cx, cy, a0) { a <- seq(a0, a0 + pi / 2, length.out = n); tibble(x = cx + r * cos(a), y = cy + r * sin(a)) } bind_rows(corner(x + w - r, y + r, -pi / 2), corner(x + w - r, y + h - r, 0), corner(x + r, y + h - r, pi / 2), corner(x + r, y + r, pi)) } ell <- function(cx, cy, rx, ry, n = 48) { t <- seq(0, 2 * pi, length.out = n); tibble(x = cx + rx * cos(t), y = cy + ry * sin(t)) } shape_poly <- function(s, mirror = FALSE) { # s = list(type, g) g <- s$g if (s$type == "e") ell(if (mirror) 200 - g[1] else g[1], g[2], g[3], g[4]) else rrect(if (mirror) 200 - g[1] - g[3] else g[1], g[2], g[3], g[4], g[5]) } E <- function(...) list(type = "e", g = c(...)); R <- function(...) list(type = "r", g = c(...)) FRONT_CENTER <- list(FHEAD = E(100, 38, 26, 32), FNECK = R(90, 68, 20, 16, 4), FCHEST = R(72, 86, 56, 50, 8), FABDO = R(74, 138, 52, 46, 6), FPELV = R(70, 186, 60, 34, 12)) FRONT_SIDED <- list(SHO = E(62, 98, 22, 15), ARM = R(40, 108, 20, 58, 9), FORE = R(36, 168, 18, 56, 8), HAND = E(40, 238, 10, 15), THIGH = R(72, 222, 27, 86, 12), KNEE = E(85, 318, 13, 12), SHIN = R(73, 332, 24, 74, 10), FOOT = E(83, 422, 15, 9)) BACK_CENTER <- list(BHEAD = E(100, 38, 26, 32), BNECK = R(90, 68, 20, 16, 4), BUPPER = R(72, 86, 56, 50, 8), BLOWER = R(74, 138, 52, 46, 6), BGLUTE = R(70, 186, 60, 34, 12)) BACK_SIDED <- list(SHO = E(62, 98, 22, 15), ARM = R(38, 108, 20, 114, 9), THIGH = R(72, 222, 27, 86, 12), CALF = R(73, 312, 24, 94, 10), HEEL = E(83, 422, 15, 9)) body_polygons <- function(gap = 40) { mk <- function(center, sided, prefix, right_on_viewer_left, dx, view) { out <- imap(center, \(s, code) shape_poly(s) |> mutate(code = code)) for (nm in names(sided)) { l <- paste0(prefix, if (right_on_viewer_left) "R" else "L", nm); r <- paste0(prefix, if (right_on_viewer_left) "L" else "R", nm) out[[l]] <- shape_poly(sided[[nm]]) |> mutate(code = l) out[[r]] <- shape_poly(sided[[nm]], TRUE) |> mutate(code = r) } bind_rows(out) |> mutate(x = x + dx, view = view) } bind_rows(mk(FRONT_CENTER, FRONT_SIDED, "F", TRUE, 0, "Front"), mk(BACK_CENTER, BACK_SIDED, "B", FALSE, 200 + gap, "Back")) |> group_by(code) |> mutate(id = cur_group_id()) |> ungroup() } body_map_plot <- function(share, label_min = 0.15, base_size = 16, legend_title = "Share of respondents", views = c("FRONT", "BACK")) { # share: named numeric (0-1) by region code poly <- body_polygons() |> mutate(share = coalesce(unname(share[code]), 0)) lab <- poly |> group_by(code, view) |> summarise(x = mean(range(x)), y = mean(range(y)), share = first(share), .groups = "drop") |> filter(share >= label_min, !code %in% c("FNECK", "BNECK")) ggplot(poly, aes(x, y, group = id, fill = share)) + geom_polygon(colour = "#818181", linewidth = .45) + geom_text(data = lab, aes(x, y, label = percent(share, 1), group = NULL), family = "Lato", fontface = "bold", size = base_size / 3.4, colour = ifelse(lab$share > .75 * max(share), "white", kds$text), inherit.aes = FALSE) + annotate("text", x = c(100, 340), y = 452, label = views, family = "Lato", fontface = "bold", size = base_size / 3.6, colour = kds$muted) + scale_fill_gradient(low = "#ffffff", high = kds$danger, limits = c(0, ceiling(max(share, na.rm = TRUE) * 10 + .01) / 10), labels = percent_format(1), name = legend_title) + scale_y_reverse() + coord_equal(clip = "off") + theme_void(base_family = "Lato", base_size = base_size) + theme(legend.position = "bottom", legend.title = element_text(colour = kds$muted, vjust = .85, margin = margin(r = 8)), legend.key.width = unit(2.2, "cm"), legend.key.height = unit(.28, "cm")) }