{"spec_id":"line-reaction-coordinate","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' line-reaction-coordinate: Reaction Coordinate Energy Diagram\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 90/100 | Created: 2026-06-24\n\nlibrary(ggplot2)\nlibrary(ragg)\n\nset.seed(42)\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(\n  \"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n  \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"\n)\n\n# Data: single-step exothermic reaction energy curve\n# Reactants at 50 kJ/mol, transition state at 120 kJ/mol, products at 20 kJ/mol\nreaction_x <- seq(0, 1, length.out = 300)\n\n# Build smooth curve via cubic spline through key points\nkey_x <- c(0, 0.15, 0.45, 0.55, 0.85, 1.0)\nkey_e <- c(50, 50, 120, 120, 20, 20)\nspl   <- splinefun(key_x, key_e, method = \"monoH.FC\")\nenergy_vals <- spl(reaction_x)\n\ndf <- data.frame(x = reaction_x, energy = energy_vals)\n\n# Key reference levels\ne_reactant   <- 50\ne_ts         <- 120\ne_product    <- 20\nx_ts         <- reaction_x[which.max(energy_vals)]\nea           <- e_ts - e_reactant   # 70 kJ/mol\ndelta_h      <- e_product - e_reactant  # -30 kJ/mol\n\n# Title length scaling\nplot_title  <- \"line-reaction-coordinate · r · ggplot2 · anyplot.ai\"\nn_chars     <- nchar(plot_title)\nbase_size   <- 12\ntitle_size  <- max(8, round(base_size * 67 / n_chars))\n\n# Plot\np <- ggplot(df, aes(x = x, y = energy)) +\n  # Dashed reference lines for reactant and product energy levels\n  geom_hline(yintercept = e_reactant, linetype = \"dashed\",\n             color = INK_MUTED, linewidth = 0.5) +\n  geom_hline(yintercept = e_product, linetype = \"dashed\",\n             color = INK_MUTED, linewidth = 0.5) +\n  # Main energy curve\n  geom_line(color = IMPRINT_PALETTE[1], linewidth = 1.4) +\n  # Transition state peak marker\n  annotate(\"point\",\n           x = x_ts, y = e_ts,\n           color = IMPRINT_PALETTE[1], size = 3, shape = 21,\n           fill = PAGE_BG, stroke = 1.2) +\n  # Activation energy arrow (Ea): vertical from reactant level to TS peak\n  annotate(\"segment\",\n           x = x_ts + 0.06, xend = x_ts + 0.06,\n           y = e_reactant, yend = e_ts,\n           arrow = arrow(ends = \"both\", length = unit(0.12, \"cm\"),\n                         type = \"closed\"),\n           color = IMPRINT_PALETTE[3], linewidth = 0.7) +\n  annotate(\"text\",\n           x = x_ts + 0.10, y = (e_reactant + e_ts) / 2,\n           label = paste0(\"Ea = \", ea, \" kJ/mol\"),\n           color = IMPRINT_PALETTE[3], size = 3.2, hjust = 0) +\n  # ΔH arrow: vertical from reactant level to product level\n  annotate(\"segment\",\n           x = 0.84, xend = 0.84,\n           y = e_reactant, yend = e_product,\n           arrow = arrow(ends = \"both\", length = unit(0.12, \"cm\"),\n                         type = \"closed\"),\n           color = IMPRINT_PALETTE[5], linewidth = 0.7) +\n  annotate(\"text\",\n           x = 0.82, y = (e_reactant + e_product) / 2,\n           label = paste0(\"ΔH = \", delta_h, \" kJ/mol\"),\n           color = IMPRINT_PALETTE[5], size = 3.2, hjust = 1) +\n  # Labels: Reactants, Transition State, Products\n  annotate(\"text\",\n           x = 0.07, y = e_reactant + 5,\n           label = \"Reactants\", color = INK, size = 3.5, fontface = \"bold\",\n           hjust = 0.5) +\n  annotate(\"text\",\n           x = x_ts, y = e_ts + 5,\n           label = \"Transition\\nState\", color = INK, size = 3.2,\n           hjust = 0.5, lineheight = 0.9) +\n  annotate(\"text\",\n           x = 0.93, y = e_product + 5,\n           label = \"Products\", color = INK, size = 3.5, fontface = \"bold\",\n           hjust = 0.5, vjust = 0) +\n  labs(\n    title = plot_title,\n    x     = \"Reaction Coordinate\",\n    y     = \"Potential Energy (kJ/mol)\"\n  ) +\n  scale_x_continuous(\n    breaks = c(0, 0.25, 0.5, 0.75, 1.0),\n    labels = c(\"0\", \"0.25\", \"0.50\", \"0.75\", \"1.0\"),\n    expand = c(0.01, 0.01)\n  ) +\n  scale_y_continuous(\n    breaks = seq(0, 130, by = 20),\n    expand = c(0.05, 0.05)\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, linewidth = 0.10),\n    panel.grid.minor  = element_blank(),\n    panel.border      = element_blank(),\n    axis.line         = element_line(color = INK_SOFT, linewidth = 0.5),\n    axis.ticks        = 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,\n                                     margin = margin(b = 10)),\n    plot.margin       = margin(t = 20, r = 80, b = 15, l = 15)\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"}