{"spec_id":"density-rug","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' density-rug: Density Plot with Rug Marks\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 95/100 | Created: 2026-05-18\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\"\nINK         <- if (THEME == \"light\") \"#1A1A17\" else \"#F0EFE8\"\nINK_SOFT    <- if (THEME == \"light\") \"#4A4A44\" else \"#B8B7B0\"\nIMPRINT   <- c(\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n                 \"#AE3030\", \"#2ABCCD\", \"#954477\")\n\n# --- Data -------------------------------------------------------------------\n# Response times (in milliseconds) from a web application\nresponse_times <- c(\n  rnorm(45, mean = 150, sd = 30),\n  rnorm(35, mean = 250, sd = 40)\n)\n\ndf <- data.frame(value = response_times)\n\n# --- Plot -------------------------------------------------------------------\np <- ggplot(df, aes(x = value)) +\n  geom_density(\n    stat = \"density\",\n    fill = IMPRINT[1],\n    color = IMPRINT[1],\n    alpha = 0.35,\n    linewidth = 1.4,\n    bw = 20\n  ) +\n  geom_density(\n    stat = \"density\",\n    fill = NA,\n    color = IMPRINT[1],\n    alpha = 1,\n    linewidth = 2,\n    bw = 20,\n    key_glyph = \"blank\"\n  ) +\n  geom_rug(\n    color = IMPRINT[1],\n    alpha = 0.7,\n    linewidth = 0.9,\n    length = unit(0.035, \"npc\")\n  ) +\n  geom_vline(\n    xintercept = 150,\n    color = IMPRINT[1],\n    linewidth = 0.6,\n    alpha = 0.5,\n    linetype = \"dashed\"\n  ) +\n  geom_vline(\n    xintercept = 250,\n    color = IMPRINT[1],\n    linewidth = 0.6,\n    alpha = 0.5,\n    linetype = \"dashed\"\n  ) +\n  annotate(\n    \"text\",\n    x = 150,\n    y = Inf,\n    label = \"Peak 1\",\n    vjust = 1.5,\n    hjust = 0.5,\n    size = 5,\n    color = INK,\n    family = \"sans\"\n  ) +\n  annotate(\n    \"text\",\n    x = 250,\n    y = Inf,\n    label = \"Peak 2\",\n    vjust = 1.5,\n    hjust = 0.5,\n    size = 5,\n    color = INK,\n    family = \"sans\"\n  ) +\n  labs(\n    title = \"density-rug · R · ggplot2 · anyplot.ai\",\n    x = \"Response Time (ms)\",\n    y = \"Density\"\n  ) +\n  scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +\n  theme_minimal(base_size = 14) +\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.3),\n    panel.grid.minor = element_blank(),\n    panel.border     = element_rect(color = INK_SOFT, fill = NA, linewidth = 0.6),\n    axis.title       = element_text(color = INK, size = 20),\n    axis.text        = element_text(color = INK_SOFT, size = 16),\n    plot.title       = element_text(color = INK, size = 24),\n    axis.ticks       = element_line(color = INK_SOFT)\n  )\n\n# --- Save -------------------------------------------------------------------\nggsave(\n  filename = sprintf(\"plot-%s.png\", THEME),\n  plot     = p,\n  device   = ragg::agg_png,\n  width    = 16,\n  height   = 9,\n  units    = \"in\",\n  dpi      = 300\n)\n"}