
3 Frequency polygons
theme_talkboy()
Graphs in this chapter use ggrewindr::theme_talkboy(), built from the Deluxe Talkboy cassette recorder: a dark walnut background, a silver frame and panel, button blue marks, and a Kaushan Script title.
3.1 Description
A frequency polygon shows a distribution as points joined by straight lines. It’s a close cousin of the histogram, but I’ve found it works better for comparing several groups on one graph. A legend tells the groups apart. We use frequency polygons to check a distribution for symmetry and skewness.
3.2 Set up
PACKAGES
Install packages.
library(ggplot2)
library(dplyr) # for tibble() and data manipulationDATA

r/nostalgia)The Talkboy was the handheld cassette recorder Kevin used in Home Alone 2 (1992), and Tiger Electronics sold the real thing that Christmas.1
We’ll simulate 300 messages recorded on a Talkboy with rgamma(), rounding each message length to a tenth of a second. Setting the seed with set.seed() means you’ll get the same 300 messages every time you run the code below.
set.seed(1999) # same 300 clips every time the code runs
talkboy_clips <- tibble(
clip_id = 1:300, # 300 different recordings, one row each
seconds = round(
rgamma(
n = 300,
shape = 4, # clip lengths average 8 seconds (with a long right tail)
rate = 0.5),
1 # one decimal place
)
)
glimpse(talkboy_clips)
#> Rows: 300
#> Columns: 2
#> $ clip_id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1…
#> $ seconds <dbl> 10.0, 6.9, 7.2, 9.0, 11.5, 10.7,…3.3 Distribution
If you ever handed a kid a tape recorder, you already know the shape of this distribution. Most messages last under 12 seconds, but every so often someone rambles on for 15 or 20. Those long messages stretch the right side of the distribution into a long tail, which makes it right skewed. Below we’ll move from the numbers, to a picture, to the formula. Then we’ll change the data and see what the unexpected looks like.
COUNT IT
Start with the first ten messages.
show/hide
head(talkboy_clips$seconds, 10)
#> [1] 10.0 6.9 7.2 9.0 11.5 10.7 5.2 3.4 14.5
#> [10] 11.1Most of these are under 12 seconds, but one (14.5) runs much longer. Long messages like this pull the mean up, while the median (the middle message) barely moves. Comparing the two is a quick way to check for skew.
show/hide
talkboy_clips |>
summarise(
mean = mean(seconds),
median = median(seconds))The mean is larger than the median, so more than half of the messages are shorter than the mean. We can count them by splitting the messages at the mean (about 8.1 seconds).
show/hide
talkboy_split <- talkboy_clips |>
mutate(speed = if_else(seconds < mean(seconds),
"shorter than the mean", "the mean or longer"),
speed = factor(speed,
levels = c("shorter than the mean", "the mean or longer"))) |>
count(speed)
talkboy_splitDRAW IT
A bar model shows the split as two parts of one whole. If the distribution were symmetric, the two parts would be about the same size. Here the short messages take up the bigger part of the bar.
show/hide
ggplot(data = talkboy_split,
aes(x = n, y = "300 messages", fill = speed)) +
geom_col(position = position_stack(reverse = TRUE),
color = "white") +
geom_text(aes(label = n), color = "white",
position = position_stack(vjust = 0.5, reverse = TRUE)) +
labs(x = "Number of messages", y = NULL,
fill = "Message length")
The frequency polygon below shows the same imbalance as a line. The dashed line marks the median and the solid line marks the mean. The tallest points sit at or left of the median, and the long tail trails off to the right, past the mean.
show/hide
ggplot(data = talkboy_clips,
aes(x = seconds)) +
geom_freqpoly() +
geom_vline(xintercept = median(talkboy_clips$seconds),
linetype = "dashed", color = "#1E1012") +
geom_vline(xintercept = mean(talkboy_clips$seconds),
color = "#1E1012") +
labs(x = "Message length (seconds)")
NAME IT
The rgamma() call that created the data is a single statement:
\[ X \sim \text{Gamma}(k = 4, \lambda = 0.5) \]
The gamma distribution has two parameters. The shape (\(k\)) controls how lopsided the polygon is, and the rate (\(\lambda\)) controls how far it stretches along the x axis. From these two numbers we can calculate the mean (\(k / \lambda = 8\) seconds), the peak (\((k - 1) / \lambda = 6\) seconds), and the standard deviation (\(\sqrt{k} / \lambda = 4\) seconds). The peak comes before the mean, which is what right skew looks like in a formula.
Our simulated messages come close: the mean is 8.1 seconds and the standard deviation is 3.8 seconds. Try raising shape in rgamma() and the polygon becomes more symmetric. A gamma distribution with a large shape looks a lot like the normal distribution from Chapter 2.
CHANGE IT
Everything above is what we expect from a kid with a tape recorder. Now we’ll swap the last 60 messages for longer pranks, like Kevin’s fake voice of his dad on the hotel phone, that run about 25 seconds each. We still expect a right skew, but we should also see something the mean and median can’t show us.
show/hide
set.seed(1999)
talkboy_pranks <- tibble(
clip_id = 1:300,
seconds = round(c(
rgamma(n = 240, shape = 4, rate = 0.5), # everyday messages
rnorm(n = 60, mean = 25, sd = 2)), # 60 long pranks
1)
)
talkboy_pranks |>
summarise(
mean = mean(seconds),
median = median(seconds))The mean climbs from 8.1 to 11.7 seconds and the median from 7.5 to 9.25. On their own, these numbers only say the skew got stronger. The polygon below tells a different story.
show/hide
ggplot(data = talkboy_pranks,
aes(x = seconds)) +
geom_freqpoly() +
geom_vline(xintercept = median(talkboy_pranks$seconds),
linetype = "dashed", color = "#1E1012") +
geom_vline(xintercept = mean(talkboy_pranks$seconds),
color = "#1E1012") +
labs(x = "Message length (seconds)")
The long tail is gone. In its place is a second peak near 25 seconds, with a valley around 20 seconds where few messages land. The mean of 11.7 seconds now describes neither group: it’s too long for an everyday message and less than half a prank. The pranks are a separate group, not a stretched tail, and a gamma distribution can’t describe them. When a polygon has two peaks, I’ve found it’s worth asking whether the data mixes two kinds of things before reporting a single mean.
3.4 Grammar
CODE
Create labels with
labs()Initialize the graph with
ggplot()and providedataMap
secondsto thexaxisAdd the
geom_freqpoly()layer
labs_freqpoly <- labs(
title = "Talkboy",
caption = "Simulated data",
subtitle = "Length of 300 recorded messages",
x = "Message length (seconds)")
ggp2_freqpoly <- ggplot(data = talkboy_clips,
aes(x = seconds)) +
geom_freqpoly()
ggp2_freqpoly +
labs_freqpolyGRAPH

Experiment to see how many bins fit your variable’s distribution.
show/hide
ggp2_freqpoly_bins45 <- ggplot(data = talkboy_clips,
aes(x = seconds)) +
geom_freqpoly(bins = 45)
ggp2_freqpoly_bins45 +
labs_freqpoly
ggp2_freqpoly_bins15 <- ggplot(data = talkboy_clips,
aes(x = seconds)) +
geom_freqpoly(bins = 15)
ggp2_freqpoly_bins15 +
labs_freqpoly

Tiger Electronics sold the first Talkboy in 1992 and the Deluxe Talkboy, with the voice changer from the film, in April 1993.↩︎