{"spec_id":"line-multi","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' line-multi: Multi-Line Comparison Plot\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 89/100 | Created: 2026-08-05\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(tidyr)\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\"\nINK      <- if (THEME == \"light\") \"#1A1A17\" else \"#F0EFE8\"\nINK_SOFT <- if (THEME == \"light\") \"#4A4A44\" else \"#B8B7B0\"\n\nIMPRINT_PALETTE <- c(\n  \"#009E73\", # 1 — brand green, always first series\n  \"#C475FD\", # 2 — lavender\n  \"#4467A3\", # 3 — blue\n  \"#BD8233\"  # 4 — ochre\n)\n\n# --- Data -----------------------------------------------------------------\n# Average daily temperature (°C) across a year for four cities\nday_of_year <- 1:365\nseasonal <- function(peak_day, amplitude, mean_temp, noise_sd) {\n  mean_temp + amplitude * sin(2 * pi * (day_of_year - peak_day) / 365) +\n    rnorm(length(day_of_year), 0, noise_sd)\n}\n\ntemps <- tibble(\n  day      = day_of_year,\n  Reykjavik = seasonal(200, 6.5, 5, 1.4),\n  Berlin    = seasonal(200, 10.5, 10, 1.6),\n  Marrakech = seasonal(200, 9.5, 20, 1.5),\n  Nairobi   = seasonal(60, 2.0, 20, 1.0)\n)\n\ndf <- temps %>%\n  pivot_longer(-day, names_to = \"city\", values_to = \"temp_c\") %>%\n  mutate(city = factor(city, levels = c(\"Reykjavik\", \"Berlin\", \"Marrakech\", \"Nairobi\")))\n\n# Smooth each series with a rolling mean to emphasize seasonal trend over daily noise\nroll_mean <- function(x, k = 7) {\n  n <- length(x)\n  sapply(seq_len(n), function(i) {\n    lo <- max(1, i - k)\n    hi <- min(n, i + k)\n    mean(x[lo:hi])\n  })\n}\n\ndf <- df %>%\n  group_by(city) %>%\n  arrange(day) %>%\n  mutate(temp_smooth = roll_mean(temp_c)) %>%\n  ungroup()\n\n# --- Plot -------------------------------------------------------------------\ntitle_text <- \"Average City Temperatures · line-multi · r · ggplot2 · anyplot.ai\"\ntitle_fontsize <- round(12 * min(1, 67 / nchar(title_text)))\n\np <- ggplot(df, aes(x = day, y = temp_smooth, color = city, linetype = city)) +\n  geom_line(linewidth = 1.1) +\n  scale_color_manual(values = IMPRINT_PALETTE) +\n  scale_linetype_manual(values = c(\"solid\", \"dashed\", \"dotted\", \"dotdash\")) +\n  scale_x_continuous(\n    breaks = c(1, 91, 182, 274, 365),\n    labels = c(\"Jan\", \"Apr\", \"Jul\", \"Oct\", \"Dec\")\n  ) +\n  labs(\n    title    = title_text,\n    x        = \"Day of Year\",\n    y        = \"Temperature (°C)\",\n    color    = \"City\",\n    linetype = \"City\"\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.x = element_blank(),\n    panel.grid.minor   = element_blank(),\n    panel.grid.major.y = element_line(color = scales::alpha(INK, 0.15), linewidth = 0.25),\n    axis.title         = element_text(color = INK, size = 10),\n    axis.text          = element_text(color = INK_SOFT, size = 8),\n    axis.ticks         = element_blank(),\n    plot.title         = element_text(color = INK, size = title_fontsize),\n    legend.background  = element_blank(),\n    legend.key         = element_blank(),\n    legend.text        = element_text(color = INK_SOFT, size = 8),\n    legend.title       = element_text(color = INK, size = 10),\n    legend.position    = \"right\"\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"}