{"spec_id":"facet-grid","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' facet-grid: Faceted Grid Plot\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 86/100 | Created: 2026-09-05\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(ragg)\n\nset.seed(42)\n\n# Theme tokens — Imprint palette (prompts/default-style-guide.md)\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\"\nIMPRINT_PALETTE <- c(\n  \"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n  \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"\n)\n\n# Data — greenhouse growth trial: response to light exposure,\n# split by plant species (rows) and soil type (columns)\nspecies_levels <- c(\"Fern\", \"Basil\", \"Clover\")\nsoil_levels    <- c(\"Sandy\", \"Clay\", \"Loamy\")\nintercepts     <- c(Fern = 2.0, Basil = 4.5, Clover = 3.2)\nslopes         <- c(Sandy = 0.9, Clay = 1.6, Loamy = 2.3)\nn_per_cell     <- 150\n\ncombos <- expand.grid(species = species_levels, soil = soil_levels,\n                       stringsAsFactors = FALSE)\n\ndf <- dplyr::bind_rows(lapply(seq_len(nrow(combos)), function(i) {\n  sp <- combos$species[i]\n  so <- combos$soil[i]\n  light_hours <- runif(n_per_cell, 4, 16)\n  growth_cm <- intercepts[[sp]] + slopes[[so]] * light_hours +\n    rnorm(n_per_cell, 0, 2.2)\n  # Floor at a small positive value — plant growth cannot be ~0 or negative.\n  growth_cm <- pmax(growth_cm, 0.3)\n  tibble::tibble(species = sp, soil = so,\n                 light_hours = light_hours, growth_cm = growth_cm)\n}))\n\ndf$species <- factor(df$species, levels = species_levels)\ndf$soil    <- factor(df$soil, levels = soil_levels)\n\n# Callout for the strongest soil/species interaction: highest intercept\n# (Basil) crossed with the steepest slope (Loamy) yields the fastest growth.\ncallout <- tibble::tibble(\n  species     = factor(\"Basil\", levels = species_levels),\n  soil        = factor(\"Loamy\", levels = soil_levels),\n  light_hours = 5.5,\n  growth_cm   = 40,\n  label       = \"Steepest growth\\nresponse\"\n)\n\n# Plot\np <- ggplot(df, aes(x = light_hours, y = growth_cm)) +\n  geom_point(color = IMPRINT_PALETTE[1], size = 1.7, alpha = 0.4) +\n  geom_smooth(method = \"lm\", formula = y ~ x, se = FALSE,\n              color = INK, linewidth = 0.9) +\n  geom_text(data = callout, aes(x = light_hours, y = growth_cm, label = label),\n            inherit.aes = FALSE, hjust = 0, lineheight = 0.9, size = 2.6,\n            fontface = \"italic\", color = INK_SOFT) +\n  facet_grid(rows = vars(species), cols = vars(soil)) +\n  labs(\n    x = \"Light Exposure (hours/day)\",\n    y = \"Growth (cm)\",\n    title = \"facet-grid · r · ggplot2 · anyplot.ai\"\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.spacing.x   = unit(1.2, \"lines\"),\n    panel.spacing.y   = unit(0.9, \"lines\"),\n    panel.grid.major  = element_line(color = INK, linewidth = 0.2),\n    panel.grid.minor  = element_blank(),\n    strip.background  = element_rect(fill = ELEVATED_BG, color = NA),\n    strip.text        = element_text(color = IMPRINT_PALETTE[1], size = 9.5,\n                                      face = \"bold\"),\n    axis.title        = element_text(color = INK, size = 10),\n    axis.text         = element_text(color = INK_SOFT, size = 7.5),\n    axis.ticks        = element_blank(),\n    plot.title        = element_text(color = INK, size = 13, face = \"bold\"),\n    plot.title.position = \"plot\"\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"}