{"spec_id":"area-elevation-profile","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' area-elevation-profile: Terrain Elevation Profile Along Transect\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 88/100 | Created: 2026-06-10\n\nlibrary(ggplot2)\nlibrary(scales)\nlibrary(ragg)\nlibrary(grid)\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\"\nIMPRINT_PALETTE <- c(\n  \"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n  \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"\n)\n\n# --- Data -------------------------------------------------------------------\n# Alpine traverse: 120 km transect, sampled every 250 m (481 points)\nctrl_km  <- c(0,    8,    18,   25,   30,   38,   45,   52,   60,   70,   78,   90,  105, 120)\nctrl_elv <- c(820, 1100, 1870, 2420, 2650, 2000, 2120, 1750, 2470, 2200, 2980, 1950, 1300, 1080)\n\nsfn      <- splinefun(ctrl_km, ctrl_elv, method = \"natural\")\ndistance <- seq(0, 120, by = 0.25)\nnoise    <- cumsum(rnorm(length(distance), 0, 3)) * 0.25\nelevation <- pmax(sfn(distance) + noise, 750)\n\ntrail <- data.frame(distance = distance, elevation = elevation)\n\n# Valley floor for ribbon base — matches the actual data minimum\nelev_floor <- min(elevation)\n\n# Key landmarks — elevation matched to the noisy profile\nlm_dist <- c(0, 18, 30, 45, 60, 78, 120)\nlm_name <- c(\"Valley Start\", \"Fontaine Col\", \"Aiguille Peak\",\n             \"Lac Hut\", \"Grand Col\", \"High Summit\", \"Valley End\")\nlm_elev <- approx(trail$distance, trail$elevation, xout = lm_dist)$y\n\nlandmarks <- data.frame(\n  name      = lm_name,\n  distance  = lm_dist,\n  elevation = lm_elev,\n  lbl_hjust = c(0, 0, 0, 0, 0, 0, 1),\n  label     = paste0(lm_name, \" (\", round(lm_elev), \"m)\")\n)\n\n# --- Title / sizing ---------------------------------------------------------\nTITLE      <- \"area-elevation-profile · r · ggplot2 · anyplot.ai\"\ntitle_size <- max(round(12 * min(1, 67 / nchar(TITLE))), 8)\n\n# --- Gradient terrain fill (ggplot2 >= 3.5.0) --------------------------------\n# Gradient from muted deep green at valley floor to bright brand green at peaks\nterrain_fill <- linearGradient(\n  colours = c(scales::alpha(\"#006B4F\", 0.40), scales::alpha(\"#009E73\", 0.65)),\n  y1 = unit(0, \"npc\"), y2 = unit(1, \"npc\")\n)\n\n# --- Plot -------------------------------------------------------------------\np <- ggplot(trail, aes(x = distance)) +\n  # Terrain silhouette — gradient fill from valley floor to profile\n  geom_ribbon(\n    aes(ymin = elev_floor, ymax = elevation),\n    fill = terrain_fill, color = NA\n  ) +\n  # Profile line\n  geom_line(\n    aes(y = elevation),\n    color = IMPRINT_PALETTE[1], linewidth = 1.0\n  ) +\n  # Landmark dashed verticals\n  geom_vline(\n    data = landmarks, aes(xintercept = distance),\n    color = INK_SOFT, linewidth = 0.4, linetype = \"dashed\"\n  ) +\n  # Landmark dots (open circle, page-bg fill for definition)\n  geom_point(\n    data = landmarks, aes(x = distance, y = elevation),\n    shape = 21, size = 2.5,\n    color = IMPRINT_PALETTE[1], fill = PAGE_BG, stroke = 1.2\n  ) +\n  # Landmark labels with name and elevation at 45° angle\n  geom_text(\n    data = landmarks,\n    aes(x = distance, y = elevation + 160, label = label, hjust = lbl_hjust),\n    color = INK, size = 3.0, vjust = 0, angle = 45\n  ) +\n  labs(\n    title    = TITLE,\n    subtitle = \"Alpine Traverse · 120 km · ~10× vertical exaggeration\",\n    x        = \"Distance (km)\",\n    y        = \"Elevation (m)\"\n  ) +\n  scale_x_continuous(\n    breaks = seq(0, 120, 20),\n    expand = expansion(mult = c(0.02, 0.04))\n  ) +\n  scale_y_continuous(\n    breaks = seq(800, 3200, 200),\n    labels = scales::comma\n  ) +\n  coord_cartesian(ylim = c(elev_floor, 3500)) +\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.y = element_line(color = INK_SOFT, linewidth = 0.2),\n    panel.grid.major.x = element_blank(),\n    panel.grid.minor   = element_blank(),\n    panel.border       = element_blank(),\n    axis.title         = element_text(color = INK, size = 10),\n    axis.text          = element_text(color = INK_SOFT, size = 8),\n    axis.line.x        = element_line(color = INK_SOFT, linewidth = 0.4),\n    axis.line.y        = element_line(color = INK_SOFT, linewidth = 0.4),\n    axis.ticks.x       = element_line(color = INK_SOFT, linewidth = 0.3),\n    axis.ticks.y       = element_blank(),\n    plot.title         = element_text(color = INK, size = title_size, hjust = 0.5),\n    plot.subtitle      = element_text(color = INK_SOFT, size = 8, hjust = 0.5),\n    plot.margin        = margin(20, 30, 15, 15, \"pt\")\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"}