Basic Bar Chart 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 Bar Chart in Python, R, Julia and JavaScript.

A vertical bar chart that displays categorical data with rectangular bars whose heights are proportional to the values they represent. This fundamental visualization is ideal for comparing discrete categories and identifying which categories have the highest or lowest values. Bar charts excel at showing rankings, distributions across categories, and making relative comparisons intuitive.

Basic Bar Chart rendered with ggplot2

Renders

R source (ggplot2)

#' anyplot.ai
#' bar-basic: Basic Bar Chart
#' Library: ggplot2 3.5.1 | R 4.4.1
#' Quality: 92/100 | Created: 2026-05-28

library(ggplot2)
library(ragg)

# 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"
GRID_COLOR  <- adjustcolor(INK, alpha.f = 0.15)

IMPRINT_PALETTE <- c(
  "#009E73", "#C475FD", "#4467A3", "#BD8233",
  "#AE3030", "#2ABCCD", "#954477", "#99B314"
)

# Data - Annual budget by department (USD millions), descending order
departments <- c(
  "Engineering", "R&D", "Operations", "Sales",
  "Marketing", "Finance", "Design", "HR"
)
budgets     <- c(48.5, 42.1, 35.7, 31.2, 22.8, 18.4, 14.9, 11.3)
mean_budget <- mean(budgets)

df <- data.frame(
  department = factor(departments, levels = departments),
  budget     = budgets,
  highlight  = ifelse(departments == "Engineering", "top", "rest")
)

# Plot
plot_title <- "bar-basic · r · ggplot2 · anyplot.ai"

p <- ggplot(df, aes(x = department, y = budget, fill = highlight)) +
  geom_col(width = 0.70) +
  geom_hline(
    yintercept = mean_budget,
    linetype   = "dashed",
    color      = INK_SOFT,
    linewidth  = 0.5
  ) +
  annotate(
    "text",
    x      = 5,
    y      = mean_budget,
    label  = sprintf("avg $%.1fM", mean_budget),
    vjust  = -0.4,
    hjust  = 0,
    size   = 2.5,
    color  = INK_SOFT
  ) +
  geom_text(
    aes(label = sprintf("$%.1fM", budget)),
    vjust = -0.5,
    size  = 2.8,
    color = INK_SOFT
  ) +
  scale_fill_manual(
    values = c(
      "top"  = IMPRINT_PALETTE[1],
      "rest" = adjustcolor(IMPRINT_PALETTE[1], alpha.f = 0.45)
    ),
    guide = "none"
  ) +
  scale_y_continuous(
    expand = expansion(mult = c(0, 0.18)),
    labels = function(x) paste0("$", x, "M")
  ) +
  labs(
    title = plot_title,
    x     = "Department",
    y     = "Annual Budget (USD Millions)"
  ) +
  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.grid.major.y = element_line(color = GRID_COLOR, linewidth = 0.4),
    panel.grid.major.x = element_blank(),
    panel.grid.minor   = element_blank(),
    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, face = "bold"),
    axis.line.x        = element_line(color = INK_SOFT, linewidth = 0.5),
    axis.line.y        = element_blank(),
    axis.ticks         = element_blank(),
    plot.margin        = margin(t = 20, r = 20, b = 10, l = 10, unit = "pt")
  )

# 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/bar-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": "bar-basic",
  "language": "r",
  "library": "ggplot2",
  "page": "https://anyplot.ai/bar-basic/r/ggplot2",
  "hub": "https://anyplot.ai/bar-basic",
  "code_json": "https://api.anyplot.ai/specs/bar-basic/ggplot2/code",
  "spec_json": "https://api.anyplot.ai/specs/bar-basic",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/bar-basic/r/ggplot2/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/bar-basic/r/ggplot2/plot-dark.png",
  "quality_score": 92.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

Part of Basic Bar Chart on anyplot.ai.

Other implementations