28  Line graphs

Graphs in this chapter use fmggp2::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 DIN Condensed title.


Requirements:

✅ a numeric (continuous) variable

✅ a numeric (date) variable

28.1 Description

A line graph shows how a number changes over time.1 The numbers go up the y axis, and time runs along the x axis.

28.2 Set up

PACKAGES:

Bring in the code we’ll use.2

show/hide
library(ggplot2)
library(dplyr) # for tibble() and data manipulation

DATA:

Batman is a movie about a man who fights bad people at night. Tim Burton made the movie, and Prince wrote songs for it. June is a month in the summer, and Batman came out on June 23, 1989. That summer the sign from the movie was all over (on things to wear, boxes of food, and a record of the songs).3 Like most very big movies,4 it made the most money in its first week and a little less every week after that. A dollar is the money people use in the United States. We’ll make up5 26 weeks of the money people paid to see the movie (in millions of dollars)6 that follow a line starting at $55 million and getting smaller by about the same part each week.7 Each week also gets a small move up or down by chance from rnorm(), because the money from a real week never lands right on the line (our first week comes out at $62.8 million).8 Starting with set.seed()9 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 joins each number to the one that came before it, so it’s made to show change. For the Batman data, the question is how fast the money for each week falls. The answer isn’t the same number of dollars each week; it’s the same part of the week before.10 Below we’ll move from the money for each week, to a graph, to the rule behind it.11

COUNT IT:

Start with the first six weeks.

show/hide
head(batman_box, 6)

The money falls about $13.8 million from week one to week two, but only about $3.7 million from week five to week six. To divide one number by another is to find how many times the second number goes into the first. The number of dollars it falls changes a lot from week to week and gets smaller over time, so instead we’ll divide each week by the week before it (ratio).12

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 is the middle number when you put all the numbers in order. The median ratio is about 0.78, so a common week keeps about 78% of the money from the week before. The 26 weeks add up to about $255 million.

DRAW IT:

On a normal y axis the line falls fast and then almost stops falling, so the later weeks are hard to read. A log scale puts 0.1, 1, and 10 at even steps, so it shows how many times bigger one number is than another.13 Below we make the same line on a log scale with scale_y_log10(). On a log scale, keeping the same part each week means falling the same space down the axis each week, so the money makes a straight line (the small ups and downs are the moves by chance).

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 money for each week follows a rule where each step keeps the same part of the last one,14 times a number picked by chance for each week:15

\[ \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 came out near 0.78. Taking the log of both sides turns the rule 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. A hill is a line that is high in the middle and low on both sides. On the log scale, the part picked by chance comes from the hill form we saw earlier in the book,16 so the number picked by chance for each week comes from a hill on the log scale.17 This is the same rule we used to make up the sizes of the pictures in Chapter 6. Try changing -0.25 to -0.10 in the code above, and the movie keeps making money for much longer.18

28.4 Grammar

CODE:

  • Make the words around the graph19 with labs()

  • Start the graph with ggplot() and give it data

  • Put week_of on the x axis and gross on the y axis

  • Add geom_line() (with a gold color and a wider linewidth, because the normal black line20 would be lost in the dark night of Batman)

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_line

GRAPH:


  1. Changes in a numeric (quantitative) variable.↩︎

  2. Install and load the packages.↩︎

  3. The bat signal was on t-shirts, hats, cereal boxes, and a soundtrack by Prince.↩︎

  4. Blockbusters.↩︎

  5. Simulate.↩︎

  6. Weekly box office gross.↩︎

  7. Shrink by about the same percent each week.↩︎

  8. A random nudge, because no week’s ticket sales land exactly on the trend.↩︎

  9. Setting the random seed.↩︎

  10. A fixed share of the previous week’s gross.↩︎

  11. The formula.↩︎

  12. The ratio of each week’s gross to the previous week’s gross.↩︎

  13. A logarithmic scale (base 10 here), where equal ratios are equal distances.↩︎

  14. Exponential decay.↩︎

  15. A random multiplier.↩︎

  16. A normal distribution.↩︎

  17. A lognormal multiplier.↩︎

  18. The movie has much longer legs.↩︎

  19. Labels.↩︎

  20. The default black line.↩︎