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.
SIMILAR CHART TYPES
Bar Chart geom_col() / geom_bar()
Compares values across discrete categories using rectangular bars proportional to values.Horizontal Bar Chart geom_col() + coord_flip()
Horizontal bars ideal for long labels, many categories, or ranked/sorted data.Grouped Bar Chart geom_bar(position = "dodge")
Clusters of bars comparing sub-groups within each category side by side.Area Chart geom_area()
Line chart with filled area beneath, emphasizing volume and magnitude of change over time.Box Plot geom_boxplot()
Shows median, quartiles, range, and outliers as a compact distribution summary.ALTERNATIVES FOR YOUR DATA TYPE
Line Chart Trend
Connected data points showing changes over a continuous or ordered dimension, usually time.Scatter Plot Relationship
Points plotted on two axes showing the relationship between two continuous variables.Bubble Chart Relationship
Scatter plot with a third variable encoded as bubble size, showing 3+ variable relationships.