{"spec_id":"contour-map-geographic","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' contour-map-geographic: Contour Lines on Geographic Map\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 92/100 | Created: 2026-05-20\n\nlibrary(ggplot2)\nlibrary(ragg)\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\"\nOCEAN_COL   <- if (THEME == \"light\") \"#C8DFF0\" else \"#1E2E3B\"\n\n# Synthetic atmospheric pressure field (North Atlantic / Europe)\n# Classic pattern: Azores High (SW), Icelandic Low (NW), European ridge (E)\nlon_seq <- seq(-30, 45, length.out = 60)\nlat_seq <- seq(35, 72, length.out = 50)\ngrid    <- expand.grid(lon = lon_seq, lat = lat_seq)\n\ngrid$pressure <- with(grid,\n  1013 +\n  12 * exp(-((lon + 28)^2 + (lat - 32)^2) / 250) +     # Azores High centre\n  (-15) * exp(-((lon + 22)^2 + (lat - 65)^2) / 200) +  # Icelandic Low centre\n  5 * exp(-((lon - 12)^2 + (lat - 51)^2) / 280) +       # European High ridge\n  rnorm(nrow(grid), 0, 0.4)\n)\n\n# 4 hPa isobar intervals — standard for synoptic weather maps\nisobar_breaks <- seq(994, 1032, by = 4)\n\n# Compute isobar label positions — midpoint of the longest segment per level\npressure_matrix <- matrix(grid$pressure, nrow = length(lon_seq), ncol = length(lat_seq))\nlabel_df <- do.call(rbind, lapply(isobar_breaks, function(level) {\n  cl <- contourLines(lon_seq, lat_seq, pressure_matrix, levels = level)\n  if (length(cl) == 0) return(NULL)\n  main <- cl[[which.max(sapply(cl, function(c) length(c$x)))]]\n  mid  <- ceiling(length(main$x) / 2)\n  data.frame(lon = main$x[mid], lat = main$y[mid], label = as.character(level))\n}))\n\n# World coastlines and political borders\nworld <- map_data(\"world\")\n\n# Plot\np <- ggplot() +\n  geom_contour_filled(\n    data   = grid,\n    aes(x = lon, y = lat, z = pressure),\n    breaks = isobar_breaks\n  ) +\n  geom_polygon(\n    data      = world,\n    aes(x = long, y = lat, group = group),\n    fill      = NA,\n    color     = INK,\n    linewidth = 0.25\n  ) +\n  geom_contour(\n    data      = grid,\n    aes(x = lon, y = lat, z = pressure),\n    breaks    = isobar_breaks,\n    color     = INK,\n    linewidth = 0.3,\n    alpha     = 0.75\n  ) +\n  geom_label(\n    data          = label_df,\n    aes(x = lon, y = lat, label = label),\n    color         = INK,\n    fill          = PAGE_BG,\n    size          = 2.2,\n    label.size    = 0.15,\n    label.padding = unit(0.1, \"lines\"),\n    fontface      = \"bold\"\n  ) +\n  coord_fixed(xlim = c(-30, 45), ylim = c(35, 72), expand = FALSE) +\n  scale_fill_viridis_d(option = \"viridis\", name = \"Pressure\\n(hPa)\") +\n  labs(\n    title = \"N. Atlantic Isobars · contour-map-geographic · r · ggplot2 · anyplot.ai\",\n    x     = \"Longitude\",\n    y     = \"Latitude\"\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 = OCEAN_COL,   color = NA),\n    panel.grid.major  = element_line(color = INK_SOFT,   linewidth = 0.15, linetype = \"dotted\"),\n    panel.grid.minor  = element_blank(),\n    panel.border      = element_rect(color = INK_SOFT,   fill = NA, linewidth = 0.4),\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 = 11, face = \"bold\"),\n    legend.background = element_rect(fill = ELEVATED_BG, color = INK_SOFT, linewidth = 0.3),\n    legend.text       = element_text(color = INK_SOFT,   size = 8),\n    legend.title      = element_text(color = INK,        size = 9),\n    legend.key.size   = unit(0.4, \"cm\")\n  )\n\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"}