For this worksheet, we need tidyverse package as well as the hibbs data from week 4.

library(tidyverse)
hibbs <- as_tibble(read.csv("../Lec11_files/hibbs.dat", sep = ""))

Problem 1: Hibbs

hibbs |>
  ggplot(mapping = aes(x = growth, y = vote)) +
  geom_smooth(method = "lm", se = F, color = "red", size = 0.5) +
  geom_hline(yintercept = 50, color = "gray") +
  geom_point() +
  ggrepel::geom_text_repel(mapping = aes(label = year)) +
  scale_x_continuous(labels = scales::label_percent(scale = 1)) +
  scale_y_continuous(labels = scales::label_percent(scale = 1)) +
  labs(x = "Avg recent growth in personal income",
       y = "Incumbent party's vote share",
       title = "Bread and Peace",
       subtitle = "Forecasting the election from the economy",
       caption = "Source: Douglas Hibbs") +
  theme_classic()

Problem 2: mpg

p <- ggplot(mpg, mapping = aes(x = cyl, y = hwy, group = cyl))
p + geom_boxplot()

Problem 3: babynames

Install and load the babynames package.

library(babynames)
  1. Create a tibble containing only the name “Robin”. First few entries are shown.
robin <- filter(babynames, name == "Robin")
head(robin, 4)
## # A tibble: 4 x 5
##    year sex   name      n      prop
##   <dbl> <chr> <chr> <int>     <dbl>
## 1  1881 M     Robin     5 0.0000462
## 2  1887 M     Robin     5 0.0000457
## 3  1888 M     Robin     6 0.0000462
## 4  1889 M     Robin     6 0.0000504
  1. Create the following plot of the number of babies named Robin.
library(babynames)
babynames |>
  filter(name == "Robin") |>
  ggplot(mapping = aes(x = year, y = n, group = sex, color = sex)) + 
    geom_line() +
    labs(x = "Year", y = "Number",
         title = "Number of babies named Robin",
         caption = "Source: SSA")