{"spec_id":"heatmap-annotated","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' heatmap-annotated: Annotated Heatmap\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 88/100 | Created: 2026-08-05\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(ragg)\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\"\nLIGHT_TEXT  <- \"#FFFDF6\"  # near-white overlay text for saturated red/blue cells\n# Light theme's near-white midpoint keeps mid-magnitude cells pale, so white text\n# needs a much higher |value| threshold there than on the dark theme's near-black midpoint.\nTEXT_THRESHOLD <- if (THEME == \"light\") 0.68 else 0.3\n\n# --- Data: correlation matrix of car performance metrics ----------------\nmetrics <- mtcars %>%\n  rename(\n    `MPG`      = mpg,\n    `Cylinders` = cyl,\n    `Displacement` = disp,\n    `Horsepower`  = hp,\n    `Rear Axle`   = drat,\n    `Weight`      = wt,\n    `Quarter Mile` = qsec\n  )\ncorr_matrix <- cor(metrics[, c(\"MPG\", \"Cylinders\", \"Displacement\", \"Horsepower\",\n                                \"Rear Axle\", \"Weight\", \"Quarter Mile\")])\n\ncorr_df <- as.data.frame(as.table(corr_matrix)) %>%\n  rename(x = Var1, y = Var2, value = Freq) %>%\n  mutate(\n    label      = sprintf(\"%.2f\", value),\n    text_color = ifelse(abs(value) > TEXT_THRESHOLD, LIGHT_TEXT, INK)\n  )\n\n# --- Plot -----------------------------------------------------------------\np <- ggplot(corr_df, aes(x = x, y = y, fill = value)) +\n  geom_tile(color = PAGE_BG, linewidth = 2) +\n  geom_text(aes(label = label, color = text_color), size = 3.4, fontface = \"bold\") +\n  scale_fill_gradient2(\n    low = \"#AE3030\", mid = PAGE_BG, high = \"#4467A3\",\n    midpoint = 0, limits = c(-1, 1), name = \"Correlation\"\n  ) +\n  scale_color_identity() +\n  coord_fixed() +\n  scale_x_discrete(position = \"top\") +\n  labs(\n    title = \"Car Performance Correlations · heatmap-annotated · r · ggplot2 · anyplot.ai\",\n    x = NULL, y = NULL\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        = element_blank(),\n    axis.text.x       = element_text(color = INK_SOFT, size = 8, angle = 30, hjust = 0),\n    axis.text.y       = element_text(color = INK_SOFT, size = 8),\n    axis.title        = element_text(color = INK, size = 10),\n    plot.title        = element_text(color = INK, size = 10, hjust = 0.5),\n    legend.background = element_rect(fill = PAGE_BG, color = NA),\n    legend.text       = element_text(color = INK_SOFT, size = 8),\n    legend.title      = element_text(color = INK, size = 10),\n    legend.position   = \"right\",\n    plot.margin       = margin(10, 10, 10, 10)\n  )\n\n# --- Save -------------------------------------------------------------------\nggsave(\n  filename = sprintf(\"plot-%s.png\", THEME),\n  plot     = p,\n  device   = ragg::agg_png,\n  width    = 6,\n  height   = 6,\n  units    = \"in\",\n  dpi      = 400\n)\n"}