{"spec_id":"map-connection-lines","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' map-connection-lines: Connection Lines Map (Origin-Destination)\n#' Library: ggplot2 4.0.3 | R 4.6.1\n#' Quality: 83/100 | Updated: 2026-11-27\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(ragg)\nif (!requireNamespace(\"maps\", quietly = TRUE)) install.packages(\"maps\", repos = \"https://cran.r-project.org\")\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(\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n                     \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\")\n\n# Imprint marks ramp (colored lines on the page), per theme\nSEQ_MARKS <- if (THEME == \"light\") {\n  list(colours = c(\"#009E73\", \"#024582\", \"#351E45\"), values = c(0, 0.687, 1))\n} else {\n  list(colours = c(\"#6F7E20\", \"#009E73\", \"#CDD9FF\"), values = c(0, 0.21, 1))\n}\n\n# Columns\nCITY       <- \"city\"          # role: label\nLAT        <- \"lat\"           # role: y\nLON        <- \"lon\"           # role: x\nPASSENGERS <- \"passengers_m\"  # role: value\n\n# Data: major international hub airports and annual passengers per route (millions)\nairports <- tibble::tibble(\n  !!CITY := c(\"New York\", \"London\", \"Tokyo\", \"Dubai\", \"Sydney\", \"Sao Paulo\",\n              \"Singapore\", \"Cape Town\", \"Mumbai\", \"Los Angeles\", \"Frankfurt\", \"Hong Kong\"),\n  !!LAT  := c(40.64, 51.48, 35.55, 25.25, -33.95, -23.43,\n              1.36, -33.96, 19.09, 33.94, 50.03, 22.31),\n  !!LON  := c(-73.78, -0.45, 139.78, 55.36, 151.17, -46.47,\n              103.99, 18.60, 72.87, -118.41, 8.57, 113.91),\n  # label placement relative to the marker\n  hjust  = c(1.1, 1.3, -0.3, -0.15, 0.5, 1.15, -0.15, 1.15, 0.4, 1.15, -0.15, -0.15),\n  vjust  = c(-1.0, 0.5, 0.5, -0.4, 1.8, 0.5, 1.4, 0.5, 1.9, 0.5, -0.5, 1.3)\n)\n\nroutes <- tibble::tibble(\n  origin = c(\"New York\", \"London\", \"Dubai\", \"Singapore\", \"Los Angeles\",\n             \"Frankfurt\", \"New York\", \"London\", \"Dubai\", \"Sao Paulo\",\n             \"Tokyo\", \"Hong Kong\", \"Mumbai\", \"Sydney\", \"Cape Town\"),\n  dest   = c(\"London\", \"Dubai\", \"Mumbai\", \"Tokyo\", \"Tokyo\",\n             \"Dubai\", \"Los Angeles\", \"Frankfurt\", \"Cape Town\", \"London\",\n             \"Los Angeles\", \"Singapore\", \"Dubai\", \"Los Angeles\", \"Dubai\"),\n  !!PASSENGERS := c(3.2, 4.8, 3.1, 2.9, 5.1, 3.6, 4.3, 2.1, 1.9, 1.4,\n                    4.7, 3.8, 2.6, 2.3, 1.1)\n)\ndf <- routes |>\n  left_join(airports[, c(CITY, LAT, LON)], by = c(\"origin\" = CITY)) |>\n  rename(origin_lat = all_of(LAT), origin_lon = all_of(LON)) |>\n  left_join(airports[, c(CITY, LAT, LON)], by = c(\"dest\" = CITY)) |>\n  rename(dest_lat = all_of(LAT), dest_lon = all_of(LON))\n\n# Plot: great-circle arcs by spherical interpolation, 80 points per route\nR_DEG <- pi / 180\nN_ARC <- 80L\narc_df <- df |>\n  mutate(\n    route = row_number(),\n    p1x = cos(origin_lat * R_DEG) * cos(origin_lon * R_DEG),\n    p1y = cos(origin_lat * R_DEG) * sin(origin_lon * R_DEG),\n    p1z = sin(origin_lat * R_DEG),\n    p2x = cos(dest_lat * R_DEG) * cos(dest_lon * R_DEG),\n    p2y = cos(dest_lat * R_DEG) * sin(dest_lon * R_DEG),\n    p2z = sin(dest_lat * R_DEG),\n    ang = acos(pmax(pmin(p1x * p2x + p1y * p2y + p1z * p2z, 1), -1))\n  ) |>\n  tidyr::crossing(t = seq(0, 1, length.out = N_ARC)) |>\n  arrange(route, t) |>\n  mutate(\n    wa = sin((1 - t) * ang) / sin(ang),\n    wb = sin(t * ang) / sin(ang),\n    ax = wa * p1x + wb * p2x,\n    ay = wa * p1y + wb * p2y,\n    az = wa * p1z + wb * p2z,\n    lon = atan2(ay, ax) / R_DEG,\n    lat = atan2(az, sqrt(ax^2 + ay^2)) / R_DEG\n  ) |>\n  group_by(route) |>\n  # a longitude jump over 180 degrees marks an antimeridian crossing: start a new segment\n  mutate(seg_id = route * 10 + cumsum(c(0, abs(diff(lon)) > 180))) |>\n  ungroup()\n\n# Base map layers in lon/lat: country polygons, graticule every 30 degrees, map outline\nlats <- seq(-90, 90, length.out = 181)\nlons <- seq(-180, 180, length.out = 361)\ngeo <- bind_rows(\n  arcs     = arc_df |> select(lon, lat, seg_id, all_of(PASSENGERS)),\n  airports = airports |> select(all_of(c(CITY, LAT, LON, \"hjust\", \"vjust\"))) |>\n    rename(lat = all_of(LAT), lon = all_of(LON)),\n  world    = map_data(\"world\") |> select(lon = long, lat, group),\n  graticule = bind_rows(\n    lapply(seq(-60, 60, by = 30), function(a) tibble::tibble(lon = lons, lat = a, g = paste0(\"lat\", a))),\n    lapply(seq(-150, 150, by = 30), function(o) tibble::tibble(lon = o, lat = lats, g = paste0(\"lon\", o)))\n  ),\n  outline  = tibble::tibble(lon = c(rep(-180, length(lats)), rep(180, length(lats))), lat = c(lats, rev(lats))),\n  .id = \"layer\"\n)\n\n# Natural Earth projection (Savric et al.): lon/lat in degrees -> planar x/y\ngeo <- geo |>\n  mutate(\n    phi  = lat * R_DEG,\n    phi2 = phi^2,\n    phi4 = phi2^2,\n    x = lon * R_DEG * (0.8707 - 0.131979 * phi2 +\n                         phi4 * (-0.013791 + phi4 * (0.003971 * phi2 - 0.001529 * phi4))),\n    y = phi * (1.007226 + phi2 * (0.015085 + phi4 * (-0.044475 + 0.028874 * phi2 - 0.005916 * phi4)))\n  )\narc_df    <- filter(geo, layer == \"arcs\")\nairports  <- filter(geo, layer == \"airports\")\nworld_map <- filter(geo, layer == \"world\")\ngraticule <- filter(geo, layer == \"graticule\")\noutline   <- filter(geo, layer == \"outline\")\n\ntitle_str  <- \"Global Flight Routes · map-connection-lines · r · ggplot2 · anyplot.ai\"\ntitle_size <- max(round(12 * 67 / nchar(title_str)), 8L)\n\np <- ggplot() +\n  geom_polygon(data = outline, aes(x = x, y = y), fill = ELEVATED_BG, color = INK_SOFT, linewidth = 0.3) +\n  geom_polygon(data = world_map, aes(x = x, y = y, group = group),\n               fill = INK_MUTED, color = NA, alpha = 0.22) +\n  geom_path(data = graticule, aes(x = x, y = y, group = g),\n            color = INK_SOFT, linewidth = 0.1, alpha = 0.3) +\n  geom_path(data = arc_df,\n            aes(x = x, y = y, group = seg_id,\n                color = .data[[PASSENGERS]], linewidth = .data[[PASSENGERS]]),\n            alpha = 0.6, lineend = \"round\") +\n  geom_point(data = airports, aes(x = x, y = y),\n             shape = 21, size = 2.8, fill = IMPRINT_PALETTE[1], color = PAGE_BG, stroke = 0.9) +\n  geom_text(data = airports, aes(x = x, y = y, label = .data[[CITY]], hjust = hjust, vjust = vjust),\n            color = INK, size = 3.1) +\n  scale_color_gradientn(\n    colours = SEQ_MARKS$colours, values = SEQ_MARKS$values,\n    name = \"Passengers\\n(millions)\",\n    guide = guide_colorbar(barwidth = 0.7, barheight = 5.0, title.position = \"top\",\n                           ticks.colour = INK_SOFT)\n  ) +\n  scale_linewidth_continuous(range = c(0.5, 2.6), guide = \"none\") +\n  coord_fixed(expand = FALSE) +\n  labs(title = title_str, x = NULL, y = NULL) +\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        = element_blank(),\n    axis.text         = element_blank(),\n    axis.title        = element_blank(),\n    axis.ticks        = element_blank(),\n    plot.title        = element_text(color = INK, size = title_size, hjust = 0.5, margin = margin(b = 6)),\n    legend.background = element_rect(fill = ELEVATED_BG, color = NA),\n    legend.text       = element_text(color = INK_SOFT, size = 8),\n    legend.title      = element_text(color = INK, size = 9),\n    legend.position   = \"right\",\n    plot.margin       = margin(8, 12, 8, 8)\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"}