{"spec_id":"phase-diagram-pt","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' phase-diagram-pt: Thermodynamic Phase Diagram (Pressure-Temperature)\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 88/100 | Created: 2026-06-08\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(tibble)\nlibrary(ragg)\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\"\nGRID_COLOR  <- adjustcolor(INK, alpha.f = 0.12)\n\nIMPRINT_PALETTE <- c(\n  \"#009E73\",  # 1 — brand green (first categorical)\n  \"#C475FD\",  # 2 — lavender\n  \"#4467A3\",  # 3 — blue\n  \"#BD8233\",  # 4 — ochre\n  \"#AE3030\",  # 5 — matte red\n  \"#2ABCCD\",  # 6 — cyan\n  \"#954477\",  # 7 — rose\n  \"#99B314\"   # 8 — lime\n)\n\n# --- Data: CO2 phase diagram (physically realistic approximations) -----------\nT_tp <- 216.55  # triple point temperature (K)\nP_tp <- 5.18    # triple point pressure (atm)\nT_c  <- 304.13  # critical point temperature (K)\nP_c  <- 72.8    # critical point pressure (atm)\n\n# Liquid-gas: Clausius-Clapeyron fit anchored at both triple and critical points\nT_lg <- seq(T_tp, T_c, length.out = 100)\nP_lg <- exp(10.825 - 1988 / T_lg)\n\n# Solid-gas: sublimation curve, L_sub/R = 25200/8.314 ≈ 3030 K\nT_sg <- seq(140, T_tp, length.out = 100)\nP_sg <- P_tp * exp(-3030.3 * (1 / T_sg - 1 / T_tp))\n\n# Solid-liquid: near-vertical melting curve, dT/dP ≈ 0.015 K/atm for CO2\nP_sl <- seq(P_tp, 2000, length.out = 100)\nT_sl <- T_tp + 0.015 * (P_sl - P_tp)\n\ndf_boundaries <- bind_rows(\n  tibble(temperature = T_lg, pressure = P_lg, boundary = \"Liquid-Gas\"),\n  tibble(temperature = T_sg, pressure = P_sg, boundary = \"Solid-Gas\"),\n  tibble(temperature = T_sl, pressure = P_sl,  boundary = \"Solid-Liquid\")\n)\ndf_boundaries$boundary <- factor(\n  df_boundaries$boundary,\n  levels = c(\"Solid-Liquid\", \"Liquid-Gas\", \"Solid-Gas\")\n)\n\ndf_special <- tibble(\n  temperature = c(T_tp, T_c),\n  pressure    = c(P_tp, P_c)\n)\n\n# --- Plot -------------------------------------------------------------------\ntitle_str <- \"CO2 Phase Diagram · phase-diagram-pt · r · ggplot2 · anyplot.ai\"\n\np <- ggplot() +\n  # Phase region fills (semi-transparent, drawn first so lines appear on top)\n  annotate(\"rect\", xmin = 130, xmax = 219, ymin = 5,     ymax = 2000,\n           fill = IMPRINT_PALETTE[3], alpha = 0.05) +\n  annotate(\"rect\", xmin = 215, xmax = 305, ymin = 5,     ymax = 75,\n           fill = IMPRINT_PALETTE[2], alpha = 0.05) +\n  annotate(\"rect\", xmin = 140, xmax = 415, ymin = 0.001, ymax = 73,\n           fill = IMPRINT_PALETTE[1], alpha = 0.05) +\n  annotate(\"rect\", xmin = 303, xmax = 415, ymin = 72,    ymax = 2000,\n           fill = IMPRINT_PALETTE[4], alpha = 0.05) +\n  geom_line(\n    data = df_boundaries,\n    aes(x = temperature, y = pressure, color = boundary, linetype = boundary),\n    linewidth = 1.2\n  ) +\n  geom_point(\n    data = df_special,\n    aes(x = temperature, y = pressure),\n    shape = 21, size = 4.5,\n    fill = ELEVATED_BG, color = INK, stroke = 1.5\n  ) +\n  # Phase region labels\n  annotate(\"text\", x = 168, y = 80,   label = \"SOLID\",\n           color = INK_MUTED, size = 4.0, fontface = \"bold\") +\n  annotate(\"text\", x = 268, y = 50,   label = \"LIQUID\",\n           color = INK_MUTED, size = 4.0, fontface = \"bold\") +\n  annotate(\"text\", x = 300, y = 0.45, label = \"GAS\",\n           color = INK_MUTED, size = 4.0, fontface = \"bold\") +\n  annotate(\"text\", x = 370, y = 600,  label = \"SUPERCRITICAL\\nFLUID\",\n           color = INK_MUTED, size = 3.5, fontface = \"bold\", lineheight = 0.9) +\n  # Special point annotations\n  annotate(\"text\", x = 221, y = 12,\n           label = \"Triple Point\\n(216.6 K, 5.2 atm)\",\n           color = INK_SOFT, size = 3.2, hjust = 0, lineheight = 0.9) +\n  annotate(\"text\", x = 308, y = 120,\n           label = \"Critical Point\\n(304.1 K, 72.8 atm)\",\n           color = INK_SOFT, size = 3.2, hjust = 0, lineheight = 0.9) +\n  scale_y_log10(\n    name   = \"Pressure (atm)\",\n    limits = c(0.001, 2000),\n    breaks = c(0.001, 0.01, 0.1, 1, 10, 100, 1000),\n    labels = c(\"0.001\", \"0.01\", \"0.1\", \"1\", \"10\", \"100\", \"1000\")\n  ) +\n  scale_x_continuous(\n    name   = \"Temperature (K)\",\n    limits = c(130, 415),\n    breaks = seq(150, 400, by = 50)\n  ) +\n  scale_color_manual(\n    values = c(\n      \"Solid-Liquid\" = IMPRINT_PALETTE[1],\n      \"Liquid-Gas\"   = IMPRINT_PALETTE[2],\n      \"Solid-Gas\"    = IMPRINT_PALETTE[3]\n    ),\n    name = \"Phase Boundary\"\n  ) +\n  scale_linetype_manual(\n    values = c(\n      \"Solid-Liquid\" = \"solid\",\n      \"Liquid-Gas\"   = \"solid\",\n      \"Solid-Gas\"    = \"dashed\"\n    ),\n    name = \"Phase Boundary\"\n  ) +\n  labs(title = title_str) +\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 = GRID_COLOR, linewidth = 0.4),\n    panel.grid.minor  = element_line(color = GRID_COLOR, linewidth = 0.2),\n    panel.border      = element_blank(),\n    axis.line         = element_line(color = INK_SOFT,  linewidth = 0.5),\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 = 12, face = \"bold\",\n                                     margin = margin(b = 8)),\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 = 10),\n    legend.position   = \"right\",\n    legend.margin     = margin(6, 8, 6, 8),\n    plot.margin       = margin(12, 15, 12, 10)\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"}