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_data <- filter(tuition_data, State == "New York")

ggplot(ny_data, aes(x = year, y = tuition, color = State, group = State)) + 
  geom_line() + 
  geom_point() +
  annotate("text", label = "New York", x = 2004.5, y = ny_data[[1,3]] + 60) + 
  labs(x = "Year", y = "Average tuition (in USD)", title = "College Tuition in New York State") + 
  xlim(2003.5, 2015.5) + theme(plot.title = element_text(hjust = 0.5)) + 
  scale_x_continuous(breaks = seq(2004, 2015, by = 1))

Lab 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.

Answer:

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

ggplot(ny_nj_data, aes(x = year, y = tuition, color = State, group = State)) + 
  geom_line() + 
  geom_point() +
  annotate("text", label = "New York", x = 2004.5, y = ny_nj_data[[1,3]] + 250) +
  annotate("text", label = "New Jersey", x = 2004.5, y = ny_nj_data[[1,3]] -250) +
  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)

gapminder <- read_csv("gapminder_DAS522.csv")
gapminder
ggplot(gapminder, aes(Fertility, LifeExp, size = Pop, colour = region)) +
  geom_point(alpha = 0.7, show.legend = T) +
  scale_size_continuous(range = c(0.5, 15), guide = "none") +
  # Here comes the gganimate specific bits
  labs(title = 'Year: {frame_time}', x = 'Fertility Rate', y = 'life expectancy') +
  xlim(0, 10) + ylim(0, 100) + theme(plot.title = element_text(hjust = 0.5)) + 
  transition_time(date) +
  ease_aes('linear') -> p

animate(p, duration = 15, fps = 4)

Lab exercise 2: Try to reproduce the following graph:

Answer:

ggplot(tuition_data, aes(tuition, State, colour = State)) +
  geom_bar(stat = "identity", alpha = 0.7, show.legend = F) +
  scale_size_continuous(range = c(0, 15000), guide = "none") +
  # Here comes the gganimate specific bits
  labs(title = 'Year: {frame_time}', x = 'Tuition amount (USD)', y = 'State') +
  xlim(0, 15000) + theme(plot.title = element_text(hjust = 0.5)) + 
  transition_time(year) +
  ease_aes('linear') -> p

animate(p, duration = 6, fps = 2)

Lab exercise 3: Try to reproduce the following graph with the diamonds data set:

Answer:

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

animate(p, fps = 5, res = 300)