Basic Box Plot in ggplot2 (R)

The same plot in 14 other libraries — Python: Altair, Bokeh, lets-plot, Matplotlib, Plotly, plotnine, Pygal, Seaborn; Julia: Makie.jl; JavaScript: Chart.js, D3.js, Apache ECharts, Highcharts, MUI X Charts. Compare all 15 side by side: Basic Box Plot in Python, R, Julia and JavaScript.

A box plot (box-and-whisker plot) showing the distribution of numerical data through quartiles. Displays the median, first and third quartiles as a box, with whiskers extending to show the data range. Essential for comparing distributions across categories and identifying outliers.

Basic Box Plot rendered with ggplot2

Renders

R source (ggplot2)

#' anyplot.ai
#' box-basic: Basic Box Plot
#' Library: ggplot2 4.0.3 | R 4.6.1
#' Quality: 84/100 | Updated: 2026-10-29

library(ggplot2)
library(scales)
library(ragg)

set.seed(42)

# --- Theme tokens -----------------------------------------------------------
THEME       <- Sys.getenv("ANYPLOT_THEME", "light")
PAGE_BG     <- if (THEME == "light") "#FAF8F1" else "#1A1A17"
ELEVATED_BG <- if (THEME == "light") "#FFFDF6" else "#242420"
INK         <- if (THEME == "light") "#1A1A17" else "#F0EFE8"
INK_SOFT    <- if (THEME == "light") "#4A4A44" else "#B8B7B0"
IMPRINT_PALETTE <- c(
    "#009E73", "#C475FD", "#4467A3", "#BD8233",
    "#AE3030", "#2ABCCD", "#954477", "#99B314"
)

# --- Columns: set these to your table's column names -----------------------
DEPARTMENT <- "Department"          # role: category
SALARY     <- "Annual Salary (USD)" # role: value

# --- Data: replace this block with your own table, e.g. df <- read.csv("salaries.csv", check.names = FALSE)
set.seed(42)
departments <- c("Engineering", "Marketing", "Sales", "Design", "Research")
dept_means  <- c(95000, 72000, 78000, 82000, 105000)
dept_sds    <- c(18000, 12000, 15000, 13000, 20000)
n_per_dept  <- 80

df <- tibble::tibble(
    !!DEPARTMENT := rep(departments, each = n_per_dept),
    !!SALARY     := unlist(lapply(seq_along(departments), function(i) {
        rnorm(n_per_dept, mean = dept_means[i], sd = dept_sds[i])
    }))
)

# --- Plot: reads only df and the constants above ----------------------------
df[[DEPARTMENT]] <- factor(df[[DEPARTMENT]], levels = unique(df[[DEPARTMENT]]))

p <- ggplot(df, aes(x = .data[[DEPARTMENT]], y = .data[[SALARY]], fill = .data[[DEPARTMENT]])) +
    # Whiskers reach 1.5 * IQR; values beyond are drawn as individual outlier points
    geom_boxplot(
        color                = INK_SOFT,
        linewidth            = 0.6,
        median.linewidth     = 1.4,
        median.colour        = INK,
        outlier.size         = 1.6,
        outlier.alpha        = 0.8,
        width                = 0.55,
        alpha                = 0.85
    ) +
    scale_fill_manual(values = IMPRINT_PALETTE, guide = "none") +
    scale_y_continuous(
        labels = label_dollar(scale = 0.001, suffix = "k"),
        expand = expansion(mult = c(0.05, 0.08))
    ) +
    labs(
        title    = "box-basic · r · ggplot2 · anyplot.ai",
        x        = DEPARTMENT,
        y        = SALARY
    ) +
    theme_minimal(base_size = 8) +
    theme(
        plot.background    = element_rect(fill = PAGE_BG, color = PAGE_BG),
        panel.background   = element_rect(fill = PAGE_BG, color = NA),
        panel.border       = element_blank(),
        panel.grid.major.y = element_line(color = INK_SOFT, linewidth = 0.2),
        panel.grid.major.x = element_blank(),
        panel.grid.minor   = element_blank(),
        axis.line.x        = element_line(color = INK_SOFT, linewidth = 0.4),
        axis.line.y        = element_line(color = INK_SOFT, linewidth = 0.4),
        axis.title         = element_text(color = INK,      size = 10),
        axis.text          = element_text(color = INK_SOFT, size = 8),
        plot.title         = element_text(
            color  = INK,
            size   = 12,
            margin = margin(b = 10)
        ),
        plot.margin = margin(t = 16, r = 20, b = 16, l = 16)
    )

# --- Save -------------------------------------------------------------------
ggsave(
    filename = sprintf("plot-%s.png", THEME),
    plot     = p,
    device   = ragg::agg_png,
    width    = 8,
    height   = 4.5,
    units    = "in",
    dpi      = 400
)

Retrieve this implementation

Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/box-basic/ggplot2/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.

{
  "spec_id": "box-basic",
  "language": "r",
  "library": "ggplot2",
  "page": "https://anyplot.ai/box-basic/r/ggplot2",
  "hub": "https://anyplot.ai/box-basic",
  "code_json": "https://api.anyplot.ai/specs/box-basic/ggplot2/code",
  "spec_json": "https://api.anyplot.ai/specs/box-basic",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/box-basic/r/ggplot2/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/box-basic/r/ggplot2/plot-dark.png",
  "quality_score": 84.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

Part of Basic Box Plot on anyplot.ai.

Other implementations