
28 Line graphs
theme_batman()
Graphs in this chapter use ggrewindr::theme_batman(), using its default "movie" palette from Batman (1989): a black background, a gold bat signal frame and title, a dark brown plot area, silver text, orange, gold, steel, rust, and pale gold marks, and a Barlow Condensed title.
28.1 Description
Line graphs display changes in a numeric variable (on the y axis) over time (on the x axis).
28.2 Set up
PACKAGES
Install packages.
show/hide
library(ggplot2)
library(dplyr) # for tibble() and data manipulationDATA
Tim Burton’s Batman opened on June 23, 1989, and the bat signal was everywhere that summer (t-shirts, hats, cereal boxes, and a soundtrack by Prince). Like most blockbusters, it made the most money in its first week and a little less every week after that.
We’ll simulate 26 weeks of box office gross (in millions of dollars) along a trend that starts at $55 million and shrinks by about the same percent each week. Each week also gets a random nudge from rnorm(), because no week’s ticket sales land exactly on the trend. That nudge is why our first week comes in at $62.8 million instead of $55 million. Setting the seed with set.seed() means you’ll get the same 26 weeks every time you run the code below.
show/hide
set.seed(1989) # same 26 weeks every time the code runs
batman_box <- tibble(
week = 1:26, # 26 weeks in theaters, one row each
week_of = as.Date("1989-06-23") + 7 * (week - 1), # first day of each week (opening was June 23)
gross = round(
55 * exp(-0.25 * (week - 1)) * # trend starts at $55 million, falling about 22% each week
exp(rnorm(26, mean = 0, sd = 0.12)), # random week to week noise
2 # two decimal places
)
)
glimpse(batman_box)
#> Rows: 26
#> Columns: 3
#> $ week <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1…
#> $ week_of <date> 1989-06-23, 1989-06-30, 1989-07…
#> $ gross <dbl> 62.78, 48.98, 26.82, 25.38, 18.8…28.3 Distribution
A line graph connects each value to the one that came before it, so it’s built to show change. For the Batman data, the question is how fast the weekly gross falls. The answer isn’t a fixed number of dollars per week; it’s a fixed share of the previous week. Below we’ll move from the box office numbers, to a picture, to the formula.
COUNT IT
Start with the first six weeks.
show/hide
head(batman_box, 6)The drop from week one to week two is about $13.8 million, but the drop from week five to week six is only about $3.7 million. The dollar drops bounce around from week to week and shrink over time, so we’ll divide each week by the week before it instead (ratio).
show/hide
batman_ratios <- batman_box |>
mutate(ratio = gross / lag(gross))
batman_ratios |>
summarise(median_ratio = median(ratio, na.rm = TRUE),
total_gross = sum(gross))The median ratio is about 0.78, which means a typical week keeps about 78% of the previous week’s gross. The 26 weeks add up to about $255 million.
DRAW IT
On a regular y axis the line drops fast and then flattens out, which makes the late weeks hard to read. Below we draw the same line on a log scale with scale_y_log10(). On a log scale, equal ratios are equal distances, so a gross that keeps the same share every week becomes a straight line (the wiggles are the random nudges).
show/hide
ggplot(data = batman_box, aes(x = week_of, y = gross)) +
geom_line(color = "#F9BB2C", linewidth = 1) +
geom_point(color = "#E96A23") +
scale_y_log10() +
labs(x = "Week of", y = "Weekly gross ($ millions, log10 scale)")
NAME IT
The weekly gross follows exponential decay with a random multiplier for each week:
\[ \text{gross}_w = 55 \, e^{-0.25 (w - 1)} \, \varepsilon_w, \quad \log(\varepsilon_w) \sim \text{Normal}(0, \; 0.12) \]
Each week keeps \(e^{-0.25} \approx 0.78\) of the week before, which is why the median ratio landed near 0.78. Taking the log of both sides turns the formula into a straight line, \(\log(\text{gross}_w) = \log(55) - 0.25(w - 1) + \log(\varepsilon_w)\), which is the line we saw on the log scale. The random part is a normal distribution on the log scale (a lognormal multiplier). The Microdrive photo sizes in Chapter 7 came from a lognormal distribution too. Try changing -0.25 to -0.10 in the code above and the movie has much longer legs.
28.4 Grammar
CODE
Create labels with
labs()Initialize the graph with
ggplot()and providedataMap
week_oftoxandgrosstoyAdd
geom_line()(with a goldcolorand a thickerlinewidth, because the default black line disappears into the Gotham night)
show/hide
labs_line <- labs(title = "Batman",
caption = "Simulated data",
subtitle = "Weekly box office, summer and fall 1989",
y = "Weekly gross ($ millions)",
x = "Week of")
ggp2_line <- ggplot(data = batman_box,
mapping = aes(x = week_of, y = gross)) +
geom_line(color = "#F9BB2C", linewidth = 1)
ggp2_line +
labs_lineGRAPH
