{"spec_id":"scatter-connected-temporal","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' scatter-connected-temporal: Connected Scatter Plot with Temporal Path\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 89/100 | Created: 2026-06-09\n\nlibrary(ggplot2)\nlibrary(scales)\nlibrary(ragg)\n\nset.seed(42)\n\n# Theme tokens (Imprint palette, theme-adaptive chrome)\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: unemployment vs inflation (Phillips curve dynamics), 1980-2019\nyears        <- 1980:2019\nunemployment <- c(\n  7.2, 7.6, 9.7, 9.6, 7.5, 7.2, 7.0, 6.2, 5.5, 5.3,\n  5.6, 6.8, 7.5, 6.9, 6.1, 5.6, 5.4, 4.9, 4.5, 4.2,\n  4.0, 4.7, 5.8, 6.0, 5.5, 5.1, 4.6, 4.6, 5.8, 9.3,\n  9.6, 8.9, 8.1, 7.4, 6.2, 5.3, 4.9, 4.4, 3.9, 3.5\n)\ninflation <- c(\n  13.5, 10.3, 6.2, 3.2, 4.3, 3.6, 1.9, 3.6, 4.1, 4.8,\n   5.4,  4.2, 3.0, 3.0, 2.6, 2.8, 2.9, 2.3, 1.6, 2.2,\n   3.4,  2.8, 1.6, 2.3, 2.7, 3.4, 3.2, 2.9, 3.8, -0.4,\n   1.6,  3.2, 2.1, 1.5, 1.6, 0.1, 1.3, 2.1, 2.4,  2.3\n)\n\ndf <- data.frame(\n  year         = years,\n  unemployment = unemployment,\n  inflation    = inflation\n)\n\nkey_years <- c(1980, 1990, 2000, 2010, 2019)\ndf_labels <- df[df$year %in% key_years, ]\n\n# Per-label positions to avoid crowding (2000 nudged left, 2019 nudged higher)\ndf_labels$lbl_x <- df_labels$unemployment + c( 0.2,  0.2, -0.3,  0.2,  0.2)\ndf_labels$lbl_y <- df_labels$inflation    + c( 0.6,  0.5,  0.5,  0.5,  0.7)\n\n# Arrow segment: from start point toward first step for temporal direction cue\narrow_start <- df[df$year == 1980, ]\narrow_end   <- df[df$year == 1981, ]\n\n# Title: scale fontsize for long title (80 chars -> size 10)\nplot_title <- \"Phillips Curve Dynamics · scatter-connected-temporal · r · ggplot2 · anyplot.ai\"\ntitle_size <- max(8L, round(12 * 67 / nchar(plot_title)))\n\n# Plot\np <- ggplot(df, aes(x = unemployment, y = inflation)) +\n  geom_path(\n    aes(color = year),\n    linewidth = 1.0,\n    alpha     = 0.85,\n    lineend   = \"round\",\n    linejoin  = \"round\"\n  ) +\n  # Start-of-path arrow to reinforce temporal direction\n  geom_segment(\n    data   = data.frame(\n      x    = arrow_start$unemployment,\n      y    = arrow_start$inflation,\n      xend = arrow_end$unemployment,\n      yend = arrow_end$inflation\n    ),\n    aes(x = x, y = y, xend = xend, yend = yend),\n    color     = IMPRINT_PALETTE[1],\n    linewidth = 1.2,\n    arrow     = arrow(length = unit(0.18, \"cm\"), type = \"closed\")\n  ) +\n  geom_point(\n    aes(color = year),\n    size  = 3.5,\n    alpha = 0.95\n  ) +\n  geom_text(\n    data     = df_labels,\n    aes(x = lbl_x, y = lbl_y, label = year),\n    color    = INK,\n    size     = 3.2,\n    fontface = \"bold\"\n  ) +\n  scale_color_gradient(\n    low   = IMPRINT_PALETTE[1],\n    high  = IMPRINT_PALETTE[3],\n    name  = \"Year\",\n    guide = guide_colorbar(\n      barwidth       = 8,\n      barheight      = 0.5,\n      title.position = \"top\",\n      title.hjust    = 0.5\n    )\n  ) +\n  scale_x_continuous(\n    labels = label_number(suffix = \"%\"),\n    expand = expansion(mult = c(0.05, 0.08))\n  ) +\n  scale_y_continuous(\n    labels = label_number(suffix = \"%\"),\n    expand = expansion(mult = c(0.05, 0.15))\n  ) +\n  labs(\n    x     = \"Unemployment Rate\",\n    y     = \"Inflation Rate\",\n    title = plot_title\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 = INK_MUTED,  linewidth = 0.15),\n    panel.grid.minor  = element_blank(),\n    panel.border      = element_blank(),\n    axis.line         = element_line(color = INK_SOFT,   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 = title_size, face = \"bold\"),\n    legend.position   = \"bottom\",\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    plot.margin       = margin(20, 20, 10, 20)\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"}