Lab Exercise

Add the data of New Jersey (including a new annotation) to the same plot so that the graph shows evolution of college tuition in New York and New Jersey in the same plot.

read_csv("us_avg_tuition.csv") -> tuition_data

tuition_data <- tuition_data %>%
  pivot_longer(cols = 2:13, names_to = "year", values_to = "tuition") %>%
  mutate(tuition = parse_number(tuition)) %>%
  tidyr::extract(year, into = "year", "^(....)") %>%
  mutate(year = as.numeric(year))

ny_nj_data <- filter(tuition_data, State %in% c("New York", "New Jersey"))

ny_line <- ny_nj_data %>% filter(State == "New York", year == 2004) %>% pull(tuition)
nj_line <- ny_nj_data %>% filter(State == "New Jersey", year == 2004) %>% pull(tuition)
ggplot(ny_nj_data, aes(x = year, y = tuition, color = State, group = State)) + 
  geom_line() + 
  geom_point() +
  annotate("text", label = "New Jersey", x = 2004.5, y = nj_line + 200) + 
  annotate("text", label = "New York", x = 2004.5, y = ny_line - 200) +
  labs(x = "Year", y = "Average tuition (in USD)", title = "College Tuition in New York & New Jersey State") + 
  xlim(2003.5, 2015.5) + theme(plot.title = element_text(hjust = 0.5)) + 
  scale_x_continuous(breaks = seq(2004, 2015, by = 1)) + 
  transition_reveal(year)

Try to reproduce the graph:

ggplot(tuition_data, aes(x = tuition, y = State, color = State)) +
  geom_col(show.legend = F) +
  labs(
    title = "Year:{round(frame_time)}",
    x = "tuition (in USD)",
    y = "State"
  ) + 
  transition_time(year)

Try to reproduce the following graph with the diamonds data set:

diamonds %>%
  ggplot(aes(x = carat, y = price, color = color)) +
  geom_point() +
  geom_smooth(se = T, color = "blue") +
  labs(
    title = "Cut Quality: {closest_state}",
    x = "Carat",
    y = "Price (USD)"
  ) +
  transition_states(cut)