{"spec_id":"histogram-2d","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' histogram-2d: 2D Histogram Heatmap\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 82/100 | Created: 2026-09-05\n\nlibrary(ggplot2)\nlibrary(tibble)\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\"\n\n# --- Data --------------------------------------------------------------------\n# Daily returns (%) for two correlated asset classes: equities vs. bonds.\nn <- 20000\nequity_returns <- rnorm(n, mean = 0.05, sd = 1.2)\nbond_returns <- 0.35 * equity_returns + rnorm(n, mean = 0.02, sd = 0.5)\n\ndf <- tibble(equity_returns = equity_returns, bond_returns = bond_returns)\n\n# --- Plot ---------------------------------------------------------------------\ntitle_text <- \"histogram-2d · r · ggplot2 · anyplot.ai\"\n\np <- ggplot(df, aes(x = equity_returns, y = bond_returns)) +\n  geom_bin2d(bins = 28) +\n  scale_fill_gradient(\n    low = \"#009E73\",\n    high = \"#4467A3\",\n    name = \"Count\",\n    trans = \"sqrt\",\n    labels = scales::label_comma()\n  ) +\n  stat_density_2d(color = INK_SOFT, linewidth = 0.25, alpha = 0.5) +\n  labs(\n    title = title_text,\n    x = \"Equity Daily Return (%)\",\n    y = \"Bond Daily Return (%)\"\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.title        = element_text(color = INK, size = 10),\n    axis.text         = element_text(color = INK_SOFT, size = 8),\n    axis.line         = element_line(color = INK_SOFT),\n    plot.title        = element_text(color = INK, size = 12),\n    legend.title      = element_text(color = INK, size = 10),\n    legend.text       = element_text(color = INK_SOFT, size = 8),\n    legend.key.height = unit(0.35, \"in\")\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"}