knitr::opts_chunk$set(echo = TRUE)
library(gganimate)
library(gifski)
library(tidyverse)
read_csv("~/Downloads/us_avg_tuition.csv") -> tuition_data
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.
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")
two_states <- filter(tuition_data, State == "New York" | State == "New Jersey")
ggplot(two_states, aes(x = year, y = tuition, color = State, group = State)) +
geom_line() +
geom_point() +
annotate("text", label = "New Jersey", x = 2004.5, y = two_states[[1,3]] + 60) +
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 / 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 following graph:
p <- ggplot(tuition_data, aes(y = State, x = tuition, color = State)) +
geom_bar(stat = "identity") +
labs(title = "Year:{frame_time}",
x = "tuition (in USD)",
y = "State") +
theme(legend.position = "none") +
transition_time(year) +
ease_aes("linear")
animate(p, duration = 12, fps = 1)
Try to reproduce the following graph:
tuition_2015 <- tuition_data %>%
group_by(State) %>%
mutate(tuition_2015 = tuition[year == 2015])
p1 <- ggplot(tuition_2015, aes(y = reorder(State, -tuition_2015), x = tuition, color = reorder(State, -tuition_2015))) +
geom_bar(stat = "identity") +
labs(title = "Year:{round(frame_time)}",
x = "tuition (in USD)",
y = "State") +
theme(legend.position = "none") +
transition_time(year) +
ease_aes("linear")
animate(p1, duration = 12, fps = 20, nframes = 100)
Try to reproduce the following graph with the diamonds data set:
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 (USD)") +
theme(plot.title = element_text(hjust = 0.5)) +
enter_fade() + exit_shrink() +
transition_states(cut) -> p4
animate(p4, fps = 5, res = 300)