
10 Overlapping bar graphs
theme_netware()
Graphs in this chapter use ggrewindr::theme_netware(), built from Novell NetWare 2.0 boxes: a NetWare red background with white text, a maroon frame, maroon, red, rose, and blush marks, and a Cabin title.
10.1 Description
Overlapping bar graphs show several groups on the same graph. The bars overlap instead of sitting side by side, so we usually make them partly transparent to let the bars behind show through. A legend tells the groups apart.
In ggplot2, we can build overlapping bar graphs using the fill argument in geom_bar() or geom_col().
10.2 Set up
PACKAGES
Install packages.
show/hide
library(ggplot2)
library(dplyr) # for tibble() and data manipulation
library(tidyr) # for uncount()DATA
Novell NetWare was the most popular network operating system of the early 90s. Office LANs grew as more desks got a PC and a network card.
We’ll simulate the number of workstations on the LAN for 24 offices in 1990, 1994, and 1998. Each year gets its own average LAN size (mean_nodes), tidyr::uncount() creates one row per office, and rnorm() draws each office’s workstation count, rounded to a whole workstation. We’ll also flag whether each office ran 25 workstations or more (lan_size). Setting the seed with set.seed() means you’ll get the same 72 offices every time you run the code below.
show/hide
set.seed(1990) # same 72 offices every time the code runs
netware_offices <- tibble(
year = c("1990", "1994", "1998"), # three survey years
mean_nodes = c(22, 24, 27) # average workstations per office (networks grow over time)
) |>
tidyr::uncount(24, .id = "office") |> # 24 offices in each year
mutate(
nodes = round(rnorm(n(), mean = mean_nodes, sd = 2.5)), # each office's workstation count
lan_size = if_else(nodes >= 25, "25 or more", "Under 25") # sort offices into two sizes
) |>
select(year, office, nodes, lan_size)
glimpse(netware_offices)
#> Rows: 72
#> Columns: 4
#> $ year <chr> "1990", "1990", "1990", "1990",…
#> $ office <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, …
#> $ nodes <dbl> 20, 23, 20, 22, 21, 26, 22, 22,…
#> $ lan_size <chr> "Under 25", "Under 25", "Under …10.3 Distribution
Overlapping bars only overlap when groups share values. Three years with very different LAN sizes would sit side by side with no stacking at all. Three years with the same LAN size would stack into a single pile.
How much the bars overlap depends on how far apart the years’ averages are compared to how much offices vary within a year. Below we’ll move from the offices, to a picture, to the formula. Then we’ll change the data and see what the graph does with an unexpected year.
COUNT IT
Start with the first six offices from 1990.
show/hide
head(netware_offices, 6)These offices ran between 20 and 26 workstations. The summary below shows each year’s mean, standard deviation, and range.
show/hide
netware_offices |>
group_by(year) |>
summarise(
mean = mean(nodes),
sd = sd(nodes),
min = min(nodes),
max = max(nodes))In each year, the largest office runs 8 to 11 more workstations than the smallest, but the means are only about 2 to 5 workstations apart. So the ranges overlap, which is what puts more than one color in the same bar.
DRAW IT
The overlapping bar graph below stacks the offices from all three years at each LAN size. The dashed lines mark the average LAN size we gave each year in rnorm(). The 1990 offices sit mostly on the left, the 1998 offices mostly on the right, and the 1994 offices land in the middle, sharing bars with both.
show/hide
ggplot(data = netware_offices,
aes(x = nodes, fill = year)) +
geom_bar() +
geom_vline(xintercept = c(22, 24, 27),
color = c("#69100A", "#A91E28", "#B3575A"),
linetype = "dashed", linewidth = 1) +
labs(x = "Workstations", y = "Offices", fill = "Year")
NAME IT
Each year’s LAN size comes from its own normal distribution with a shared standard deviation:
\[ X_{y} \sim \text{Normal}(\mu_y, \sigma = 2.5), \quad \mu_{1990} = 22, \; \mu_{1994} = 24, \; \mu_{1998} = 27 \]
The overlap depends on the distance between two means measured in standard deviations. The 1990 and 1998 means are \((27 - 22) / 2.5 = 2\) standard deviations apart, and the 1994 and 1998 means are only \((27 - 24) / 2.5 = 1.2\) apart. One way to put a number on that overlap is to pick a random office from each year and ask how often the “smaller” year wins:
\[ P(X_{1994} > X_{1998}) = \Phi\left(\frac{24 - 27}{2.5\sqrt{2}}\right) \approx 0.20 \]
So a 1994 office runs more workstations than a 1998 office about 20% of the time, while a 1990 office does so only about 8% of the time. Try moving the three means in mean_nodes closer together and the bars stack into a single colorful pile.
CHANGE IT
So far the graph shows what we expect: each year is one pile, and the piles move right as LANs grow. Now we’ll change the 1998 offices. Suppose the head offices kept growing to about 32 workstations, but the branch offices stayed small at about 21. We expect two 1998 piles, one far right and one back on top of the 1990 offices, with almost nothing near the 1998 average.
show/hide
set.seed(1990) # same seed as above
netware_split <- netware_offices |>
mutate(
split_mean = if_else(office <= 12, 32, 21), # 12 head offices, 12 branches
nodes = if_else(year == "1998",
round(rnorm(n(), mean = split_mean, sd = 1.5)), nodes),
lan_size = if_else(nodes >= 25, "25 or more", "Under 25")) |>
select(-split_mean)
netware_split |>
group_by(year) |>
summarise(mean = mean(nodes), sd = sd(nodes),
min = min(nodes), max = max(nodes))The 1998 mean barely moved, to 26.2 workstations, but its standard deviation jumped to 6.2. We’ll redraw the same graph below with the same dashed lines.
show/hide
ggplot(data = netware_split,
aes(x = nodes, fill = year)) +
geom_bar() +
geom_vline(xintercept = c(22, 24, 27),
color = c("#69100A", "#A91E28", "#B3575A"),
linetype = "dashed", linewidth = 1) +
labs(x = "Workstations", y = "Offices", fill = "Year")
The 1998 dashed line now sits in an empty stretch. None of the 1998 offices ran between 24 and 29 workstations. The 12 branch offices pile up with the 1990 offices on the left, and the head offices sit alone on the right.
The mean says 1998 offices were a little bigger than 1994 offices, but the bars show that no 1998 office was typical. I’ve found this is the reason to look at the bars before comparing group means. A mean can’t tell us when one group has split in two, but the overlapping colors can.
10.4 Grammar
CODE
Create labels with
labs()Initialize the graph with
ggplot()and providedataMap
nodesto thexandyeartofillAdd the
geom_bar()layer withposition = "identity"(so the bars overlap instead of stacking) andalpha = 0.5(so the bars behind show through)
show/hide
labs_bar_ovrlp <- labs(
title = "Novell NetWare",
caption = "Simulated data",
subtitle = "Workstations per office LAN in three years",
x = "Workstations",
y = "Offices",
fill = "Year")
ggp2_bar_ovrlp <- ggplot(data = netware_offices,
aes(x = nodes, fill = year)) +
geom_bar(position = "identity", alpha = 0.5)
ggp2_bar_ovrlp +
labs_bar_ovrlpGRAPH

10.5 More info
Overlapping bar graphs can also be built with
geom_col().geom_bar()has additional options for arranging overlapping bars. We can set thepositionargument to"dodge"or"dodge2", depending on how we’d like the data displayed.
10.5.1 geom_col()
To build an overlapping bar graph with geom_col(), we need to create a column with the counts for nodes and year in the dataset.
- Create the
netware_coldata:
show/hide
netware_col <- netware_offices |>
count(year, nodes, name = "Offices")- Map the counts to the
yaxis,nodesto thexaxis, andyeartofill, and use the samepositionandalphaingeom_col()
show/hide
labs_col_ovrlp <- labs(
title = "Novell NetWare",
caption = "Simulated data",
subtitle = "built with 'geom_col()'",
x = "Workstations",
y = "Offices",
fill = "Year")
ggp2_col_ovrlp <- ggplot(data = netware_col,
mapping = aes(y = Offices,
x = nodes,
fill = year)) +
geom_col(position = "identity", alpha = 0.5)
ggp2_col_ovrlp +
labs_col_ovrlp
Compare the two graphs below:


10.5.2 dodge
position = "dodge"preserves the vertical position of a geom while adjusting the horizontal position"dodge"requires the grouping variable to be specified in the global orgeom_ layer
We’ll use the lan_size column, which splits offices into those with 25 workstations or more and those with fewer.
show/hide
count(netware_offices, lan_size, year)Create labels with
labs()Initialize the graph with
ggplot()and providedataMap
lan_sizeto thexandyeartogroupandfillInside the
geom_bar()function, setpositionto"dodge"
show/hide
labs_bar_dodge <- labs(
title = "Novell NetWare",
caption = "Simulated data",
subtitle = "position = 'dodge'",
x = "LAN size",
y = "Offices",
fill = "Year")
ggp2_bar_dodge <- ggplot(data = netware_offices,
aes(x = lan_size,
group = year,
fill = year)) +
geom_bar(
position = "dodge")
ggp2_bar_dodge +
labs_bar_dodge
10.5.3 dodge2
Create labels with
labs()Initialize the graph with
ggplot()and providedataMap
lan_sizetoxandyeartofillInside
geom_bar(), setpositionto"dodge2"
"dodge2"works without a grouping variable in a layer"dodge2"works with bars and rectangles"dodge2"is useful for arranging graphs with variable widths.
show/hide
labs_bar_dodge2 <- labs(
title = "Novell NetWare",
caption = "Simulated data",
subtitle = "position = 'dodge2'",
x = "LAN size",
y = "Offices",
fill = "Year")
ggp2_bar_dodge2 <- ggplot(data = netware_offices,
aes(x = lan_size,
fill = year)) +
geom_bar(
position = "dodge2")
ggp2_bar_dodge2 +
labs_bar_dodge2
Compare the two graphs below:

