{"spec_id":"bland-altman-basic","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' bland-altman-basic: Bland-Altman Agreement Plot\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 88/100 | Created: 2026-08-11\n\nlibrary(ggplot2)\nlibrary(tibble)\nlibrary(ragg)\nlibrary(scales)\n\nset.seed(42)\n\n# --- Theme tokens -----------------------------------------------------------\nTHEME       <- Sys.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG     <- if (THEME == \"light\") \"#FAF8F1\" else \"#1A1A17\"\nELEVATED_BG <- if (THEME == \"light\") \"#FFFDF6\" else \"#242420\"\nINK         <- if (THEME == \"light\") \"#1A1A17\" else \"#F0EFE8\"\nINK_SOFT    <- if (THEME == \"light\") \"#4A4A44\" else \"#B8B7B0\"\nINK_MUTED   <- if (THEME == \"light\") \"#6B6A63\" else \"#A8A79F\"\n\nIMPRINT_PALETTE <- c(\n  \"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n  \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"\n)\n\n# --- Data ---------------------------------------------------------------\n# Systolic blood pressure (mmHg) from two sphygmomanometers on 90 subjects\nn <- 90\ntrue_sbp <- rnorm(n, mean = 128, sd = 15)\ndevice_a <- true_sbp + rnorm(n, mean = 0, sd = 4)\ndevice_b <- true_sbp + rnorm(n, mean = 2.5, sd = 5)\n\navg_sbp <- (device_a + device_b) / 2\ndiff_sbp <- device_a - device_b\n\nbias <- mean(diff_sbp)\nsd_diff <- sd(diff_sbp)\nloa_upper <- bias + 1.96 * sd_diff\nloa_lower <- bias - 1.96 * sd_diff\n\ndf <- tibble(avg_sbp = avg_sbp, diff_sbp = diff_sbp)\nloa_band <- tibble(x = range(avg_sbp))\n\nannotation_x <- max(avg_sbp) - 0.02 * diff(range(avg_sbp))\n\n# --- Plot -----------------------------------------------------------------\np <- ggplot(df, aes(x = avg_sbp, y = diff_sbp)) +\n  geom_ribbon(\n    data = loa_band, aes(x = x, ymin = loa_lower, ymax = loa_upper),\n    inherit.aes = FALSE, fill = IMPRINT_PALETTE[5], alpha = 0.08\n  ) +\n  geom_hline(yintercept = 0, color = INK_MUTED, linewidth = 0.4, linetype = \"solid\") +\n  geom_hline(yintercept = bias, color = IMPRINT_PALETTE[3], linewidth = 1.1) +\n  geom_hline(yintercept = loa_upper, color = IMPRINT_PALETTE[5], linewidth = 0.9, linetype = \"dashed\") +\n  geom_hline(yintercept = loa_lower, color = IMPRINT_PALETTE[5], linewidth = 0.9, linetype = \"dashed\") +\n  geom_point(color = IMPRINT_PALETTE[1], size = 2.5, alpha = 0.6) +\n  annotate(\n    \"text\", x = annotation_x, y = bias, hjust = 1, vjust = -0.6,\n    label = sprintf(\"Bias = %.1f mmHg\", bias),\n    size = 4, color = IMPRINT_PALETTE[3]\n  ) +\n  annotate(\n    \"text\", x = annotation_x, y = loa_upper, hjust = 1, vjust = -0.6,\n    label = sprintf(\"+1.96 SD = %.1f\", loa_upper),\n    size = 4, color = IMPRINT_PALETTE[5]\n  ) +\n  annotate(\n    \"text\", x = annotation_x, y = loa_lower, hjust = 1, vjust = 1.4,\n    label = sprintf(\"-1.96 SD = %.1f\", loa_lower),\n    size = 4, color = IMPRINT_PALETTE[5]\n  ) +\n  labs(\n    title = \"bland-altman-basic · r · ggplot2 · anyplot.ai\",\n    x = \"Mean of Device A and Device B (mmHg)\",\n    y = \"Difference: Device A − Device B (mmHg)\"\n  ) +\n  theme_minimal(base_size = 8) +\n  theme(\n    plot.background   = element_rect(fill = PAGE_BG, color = PAGE_BG),\n    panel.background  = element_rect(fill = PAGE_BG, color = NA),\n    panel.grid.major  = element_line(color = alpha(INK, 0.15), linewidth = 0.4),\n    panel.grid.minor  = element_blank(),\n    axis.line         = element_line(color = INK_SOFT, linewidth = 0.4),\n    axis.ticks        = element_blank(),\n    axis.title        = element_text(color = INK, size = 10),\n    axis.text         = element_text(color = INK_SOFT, size = 8),\n    plot.title        = element_text(color = INK, size = 12, face = \"bold\"),\n    plot.margin       = margin(t = 14, r = 24, b = 10, l = 10)\n  )\n\n# --- Save -------------------------------------------------------------------\nggsave(\n  filename = sprintf(\"plot-%s.png\", THEME),\n  plot     = p,\n  device   = ragg::agg_png,\n  width    = 8,\n  height   = 4.5,\n  units    = \"in\",\n  dpi      = 400\n)\n"}