
1 Bar graphs
theme_gameboy()
Graphs in this chapter use ggrewindr::theme_gameboy(), modeled on the original Game Boy screen: the four shade green palette, a dark green frame, and the pixel font Press Start 2P (bundled with the package).
1.1 Description
A bar graph compares data in different categories using rectangular bars that vary in length or height. The bars can be vertical or horizontal. With vertical bars (which we’ll cover here), the vertical axis shows the quantities being measured and the horizontal axis lists the categories. Bar graphs often include a legend explaining the colors or patterns used when comparing multiple variables.
In ggplot2, we build bar graphs with geom_bar().
1.2 Set up
PACKAGES
Install packages.
show/hide
library(ggplot2)
library(dplyr) # for tibble() and data manipulationDATA

Alleyway was one of the launch games for the Game Boy in 1989. Mario steers a horizontal, pill-shaped paddle along the bottom of the screen and bounces a ball into rows of bricks. A game ends when the last ball is lost.

We’ll simulate the last stage reached by 300 players with sample(), giving each of the first three stages its own probability. Setting the seed with set.seed() means you’ll get the same 300 games every time you run the code below.
show/hide
set.seed(1998) # same 300 games every time the code runs
alleyway_games <- tibble(
player_id = 1:300, # 300 different players, one row (and one game) each
stage = sample(
c("Stage 1", "Stage 2", "Stage 3"), # stages where the last ball can be lost
size = 300, # one stage for each player
replace = TRUE, # stages can repeat across players
prob = c(0.30, 0.42, 0.28) # probabilities for each stage (Stage 2 is most likely)
)
)
glimpse(alleyway_games)
#> Rows: 300
#> Columns: 2
#> $ player_id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,…
#> $ stage <chr> "Stage 2", "Stage 2", "Stage 2…1.3 Distribution
The histograms and frequency polygons in the following chapters count numbers that fall into bins. A bar graph is simpler: it counts how many times each category shows up. Because we chose the probabilities in sample(), we know what those counts should be before we draw anything. Below we’ll move from the choices, to a picture, to the formula.
COUNT IT
Start with the first ten players.
show/hide
head(alleyway_games$stage, 10)
#> [1] "Stage 2" "Stage 2" "Stage 2" "Stage 2"
#> [5] "Stage 3" "Stage 2" "Stage 2" "Stage 3"
#> [9] "Stage 3" "Stage 1"Six of these ten players lost their last ball in Stage 2. Ten games is too few to trust, so we count all 300. Then we compare each count to the count we’d expect: the number of players (300) times the probability we gave each stage.
show/hide
stage_counts <- alleyway_games |>
count(stage) |>
mutate(expected = 300 * c(0.30, 0.42, 0.28))
stage_countsDRAW IT
A bar model shows the three counts as parts of one whole. The full bar is all 300 players, and Stage 2 takes up the biggest part.
show/hide
ggplot(data = stage_counts,
aes(x = n, y = "300 players", fill = stage)) +
geom_col(
position = position_stack(reverse = TRUE),
show.legend = FALSE) +
geom_text(
aes(label = paste(stage, n, sep = "\n")),
color = c("#9BBC0F", "#9BBC0F", "#0F380F"),
position = position_stack(vjust = 0.5, reverse = TRUE)
) +
labs(
x = "Number of players",
y = NULL)
A bar graph takes the same three parts, stands each one upright, and puts them side by side. The dashed lines below mark the expected counts. Each bar lands near its line, and the Stage 2 bar matches its line exactly.
show/hide
ggplot(data = alleyway_games,
aes(x = stage)) +
geom_bar(aes(fill = stage), show.legend = FALSE) +
geom_errorbar(
data = stage_counts,
aes(
y = expected,
ymin = expected,
ymax = expected),
linetype = "dashed",
width = 0.9) +
labs(x = "Stage", y = "Count")
NAME IT
Each game is like rolling an uneven three sided die. Counting the results of 300 rolls gives a multinomial distribution:
\[ (N_{\text{Stage 1}}, N_{\text{Stage 2}}, N_{\text{Stage 3}}) \sim \text{Multinomial}(n = 300, \; p = (0.30, 0.42, 0.28)) \]
The expected height of each bar is \(n \times p\): \(300 \times 0.30 = 90\) for Stage 1, \(300 \times 0.42 = 126\) for Stage 2, and \(300 \times 0.28 = 84\) for Stage 3. The three probabilities add to 1, so the three bars always add to 300. If one bar comes out taller than expected, another has to come out shorter.
Our simulated players show exactly that trade off: Stage 1 came in 6 below its expected count, and Stage 3 came in 6 above. Change the number in set.seed() and the bars move around their dashed lines, but they always add to 300.
CHANGE IT
Everything so far is what we expected: 300 games, and bars that land near their dashed lines. Now we keep the same three probabilities but record only 30 games. The expected counts shrink to 9, 12.6, and 8.4, and I expect the bars to wander much farther from them.
show/hide
set.seed(1998)
alleyway_games_30 <- tibble(
player_id = 1:30,
stage = sample(c("Stage 1", "Stage 2", "Stage 3"),
size = 30, replace = TRUE, prob = c(0.30, 0.42, 0.28))
)
stage_counts_30 <- alleyway_games_30 |>
count(stage) |>
mutate(expected = 30 * c(0.30, 0.42, 0.28))
stage_counts_30show/hide
ggplot(data = alleyway_games_30,
aes(x = stage)) +
geom_bar(aes(fill = stage), show.legend = FALSE) +
geom_errorbar(
data = stage_counts_30,
aes(
y = expected,
ymin = expected,
ymax = expected),
linetype = "dashed",
width = 0.9) +
labs(x = "Stage", y = "Count")
The Stage 1 bar above stops at 5 players, almost half of its expected 9. Stage 2 overshoots with 16 players instead of 12.6. Nothing about the game changed, only the number of games. With 300 players, Stage 1 missed its line by about 7%; with 30, it missed by about 44%.
I’ve found this is the first thing to check when one bar looks surprisingly tall or short: how many observations sit behind it. Small samples make bar heights jumpy, so a gap between two short bars can come from chance alone.
1.4 Grammar
CODE
Create labels with
labs()Initialize the graph with
ggplot()and providedataMap
stageto thexaxisAdd
geom_bar()
show/hide
labs_bar <- labs(
title = "Alleyway",
caption = "Simulated data",
subtitle = "Last stage reached by 300 players",
x = "Stage", y = "Count")
ggp2_bar <- ggplot(data = alleyway_games,
aes(x = stage)) +
geom_bar()
ggp2_bar +
labs_barGRAPH

1.5 More info
The connection between statistical transformations and geoms is an important principle for building graphs (and mastering the grammar) with ggplot2. Below we cover why geom_bar(stat = "count") produces the same result as stat_count(geom = "bar").1
“every geom has a default stat, and every stat a default geom.” (ggplot2 book)
1.5.1 Stats and geoms
stat_count()
The default stat argument in geom_bar() is set to "count", which ‘counts the number of cases at each x position’, so it’s ideal for categorical variables (or factors).
We can also build bar graphs with stat_count() by setting its geom argument.
The link between geom_geom_name(stat = "stat_name") and stat_stat_name(geom = "geom_name") is shown below:
show/hide
ggp2_geom_bar <- ggplot(data = alleyway_games,
aes(x = stage)) +
geom_bar(aes(fill = stage),
stat = "count", show.legend = FALSE) +
labs(title = "geom_bar(stat = 'count')")
ggp2_geom_bar
ggp2_stat_count <- ggplot(data = alleyway_games,
aes(x = stage)) +
stat_count(aes(fill = stage),
geom = "bar", show.legend = FALSE) +
labs(title = "stat_count(geom = 'bar')")
ggp2_stat_count

1.5.2 geom_col()
geom_col()
geom_col() doesn’t count anything for us, so we compute a count column first and then map it to the y aesthetic.
show/hide
alleyway_games |>
# create column of counts
dplyr::count(stage, name = "count") |>
# map into x and y
ggplot(mapping = aes(x = stage, y = count)) +
geom_col() +
labs_bar +
labs(caption = "geom_col()")
# compare to geom_bar()
ggp2_bar +
labs_bar

Bar graphs can also be created with
geom_col()↩︎