Get Started →
CompositionComparison CategoricalNumerical

Stacked Bar Chart

ggplot2: geom_bar(position = "stack") · Package: ggplot2 · Variables: 2 categorical + 1 numerical

WHAT IS A STACKED BAR CHART?

A stacked bar chart divides each bar into colored segments representing sub-groups, showing both the total value and its composition at once. It answers questions like "what is the total per category, and how is that total broken down?" For example, total revenue per quarter broken down by product line, or total headcount per department by seniority level. Stacked bars work best with 2-5 segments. For more segments, consider a treemap. In ggplot2, use geom_bar(position = "stack") with a fill aesthetic.

BEST FOR

  • · Part-to-whole within categories
  • · Total and breakdown simultaneously
  • · 2-5 segments per bar

AVOID WHEN

  • · Comparing individual sub-group values across categories
  • · More than 6 segments

R + GGPLOT2 CODE EXAMPLE

ggplot2
ggplot(mtcars, aes(x = factor(cyl), fill = factor(gear))) +
  geom_bar(position = "stack") +
  labs(title = "Gears within Cylinder Groups", fill = "Gears")

Run this code now

Paste the code above into RChat and see the stacked bar chart rendered instantly in your browser.

Try RChat Free →

SIMILAR CHART TYPES

ALTERNATIVES FOR YOUR DATA TYPE