Exercise 1

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("/Users/yuhe/Downloads/us_avg_tuition.csv") -> tuition_data
## Rows: 50 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (13): State, 2004-05, 2005-06, 2006-07, 2007-08, 2008-09, 2009-10, 2010-...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
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))

nyj_data <- filter(tuition_data, State == "New Jersey" | State == "New York")

ggplot(nyj_data, aes(x = year, y = tuition, color = State, group = State)) + 
  geom_line() + 
  geom_point() +
  labs(x = "Year", y = "Average tuition (in USD)", title = "College Tuition in New York and New Jersey") + 
  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)
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?

Exercise 2

ggplot(tuition_data, aes(x = tuition, y = State, color = State)) +
  geom_col() +
  labs(x = "Tuition (in USD)", y = "State", title = 'Year: {as.integer(frame_time)}') +
  theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
  transition_time(year)

Exercise 2 optional

ggplot(tuition_data, aes(x = tuition, y = fct_reorder2(State,year,tuition), color = State)) +
  geom_col() +
  labs(x = "Tuition (in USD)", y = "State", title = 'Year: {as.integer(frame_time)}') +
  theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
  transition_time(year)

Exercise 3

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point(aes(color = color)) +
  geom_smooth() +
  labs(x = "Carat", y = "Price (USD)", title = "Cut Quality: {closest_state}") +
  theme(plot.title = element_text(hjust = 0.5)) +
  enter_fade() +
  exit_shrink() +
  transition_states(cut) -> p

animate(p, fps = 5, res = 100)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'