{"spec_id":"spectrum-nmr","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' spectrum-nmr: NMR Spectrum (Nuclear Magnetic Resonance)\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 89/100 | Created: 2026-06-03\n\nlibrary(ggplot2)\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(\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n                     \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\")\n\n# Simulated 1H NMR spectrum of ethanol (CH3CH2OH) at 400 MHz\nppm <- seq(5.2, -0.5, length.out = 7000)\n\n# J-coupling constant: 7 Hz at 400 MHz = 0.0175 ppm\nJ        <- 0.0175\nhw_sharp <- 0.004   # sharp peak half-width (ppm)\nhw_broad <- 0.030   # broad OH singlet half-width (ppm)\n\n# CH3 triplet at 1.17 ppm (3H): 1:2:1 intensity ratio\nch3_ppm <- c(1.17 - J, 1.17, 1.17 + J)\nch3_h   <- c(0.55, 1.10, 0.55)\n\n# CH2 quartet at 3.69 ppm (2H): 1:3:3:1 intensity ratio\nch2_ppm <- c(3.69 - 1.5 * J, 3.69 - 0.5 * J, 3.69 + 0.5 * J, 3.69 + 1.5 * J)\nch2_h   <- c(0.22, 0.65, 0.65, 0.22)\n\n# OH singlet at 2.60 ppm (1H, broad) + TMS reference at 0.00 ppm\nintensity <- numeric(length(ppm))\nfor (i in seq_along(ch3_ppm)) {\n  intensity <- intensity + ch3_h[i] * (hw_sharp / 2)^2 / ((ppm - ch3_ppm[i])^2 + (hw_sharp / 2)^2)\n}\nfor (i in seq_along(ch2_ppm)) {\n  intensity <- intensity + ch2_h[i] * (hw_sharp / 2)^2 / ((ppm - ch2_ppm[i])^2 + (hw_sharp / 2)^2)\n}\nintensity <- intensity + 0.35 * (hw_broad / 2)^2 / ((ppm - 2.60)^2 + (hw_broad / 2)^2)\nintensity <- intensity + 0.40 * (hw_sharp / 2)^2 / ((ppm - 0.00)^2 + (hw_sharp / 2)^2)\nintensity <- intensity + rnorm(length(ppm), 0, 0.003)\nintensity <- pmax(intensity, 0)\n\ndf <- data.frame(ppm = ppm, intensity = intensity)\n\n# Title length check — only shrink when > 67 chars baseline\nplot_title <- \"Ethanol ¹H NMR · spectrum-nmr · r · ggplot2 · anyplot.ai\"\ntitle_n    <- nchar(plot_title)\ntitle_size <- max(8, round(12 * min(1.0, 67 / title_n)))\n\np <- ggplot(df, aes(x = ppm, y = intensity)) +\n  geom_hline(yintercept = 0, color = INK_SOFT, linewidth = 0.35) +\n  geom_line(color = IMPRINT_PALETTE[1], linewidth = 0.65) +\n  annotate(\"label\", x = 1.17, y = 1.33,\n           label = \"CH₃  1.17 ppm\\ntriplet  (3H)\",\n           color = INK_MUTED, fill = PAGE_BG, label.size = 0,\n           size = 3.0, hjust = 0.5, lineheight = 0.9) +\n  annotate(\"label\", x = 3.69, y = 0.87,\n           label = \"CH₂  3.69 ppm\\nquartet  (2H)\",\n           color = INK_MUTED, fill = PAGE_BG, label.size = 0,\n           size = 3.0, hjust = 0.5, lineheight = 0.9) +\n  annotate(\"label\", x = 2.60, y = 0.59,\n           label = \"OH  2.60 ppm\\nsinglet  (1H)\",\n           color = INK_MUTED, fill = PAGE_BG, label.size = 0,\n           size = 3.0, hjust = 0.5, lineheight = 0.9) +\n  annotate(\"label\", x = 0.00, y = 0.64,\n           label = \"TMS  0.00 ppm\\n(reference)\",\n           color = INK_MUTED, fill = PAGE_BG, label.size = 0,\n           size = 3.0, hjust = 0.5, lineheight = 0.9) +\n  scale_x_reverse(breaks = seq(5, 0, by = -1)) +\n  scale_y_continuous(\n    limits = c(-0.05, 1.75),\n    breaks = c(0, 0.5, 1.0),\n    expand = c(0, 0)\n  ) +\n  labs(\n    title = plot_title,\n    x     = \"Chemical Shift (δ, ppm)\",\n    y     = \"Intensity (a.u.)\"\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_blank(),\n    panel.grid.minor  = element_blank(),\n    panel.border      = element_blank(),\n    axis.line.x       = element_line(color = INK_SOFT,  linewidth = 0.5),\n    axis.line.y       = 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    axis.text.y       = element_text(color = INK_MUTED, size = 7),\n    plot.title        = element_text(color = INK,       size = title_size, face = \"bold\"),\n    plot.margin       = margin(20, 30, 15, 20, unit = \"pt\")\n  )\n\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"}