AE 14: Debugging R code

Suggested answers

Application exercise
Answers

Packages

We will use the following packages in this application exercise.

  • tidyverse: For data import, wrangling, and visualization.
  • babynames: For working with the Social Security Administration’s baby names data.
library(tidyverse)
library(babynames)

Plot the total number of U.S. births over time using a stacked area chart

applicants |>
  mutate(
    sex = if_else(sex == "F", "Female", "Male"),
    n_all = n_all / 1e06
  ) |>
  ggplot(mapping = aes(x = year, y = n_all, fill = sex)) +
  geom_area() +
  scale_fill_brewer(type = "qual") +
  labs(
    title = "Total US births",
    x = "Year",
    y = "Millions",
    fill = NULL,
    caption = "Source: Social Security Administration"
  ) +
  theme_minimal()
1
Use geom_area()