Loading Libraries:
knitr::opts_chunk$set(warning = FALSE, fig.align = "center", out.width = "85%",
message = FALSE, cache = TRUE)
library(openintro)
library(gganimate)
library(gifski)
Lab 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("~/Desktop/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))
selected_data <- filter(tuition_data, State %in% c("New York", "New Jersey"))
p <- ggplot(selected_data, aes(x = year, y = tuition, color = State, group = State)) +
geom_line() +
geom_point() +
annotate("text", label = "New York", x = 2004.5,
y = selected_data[[1,3]] + -3400, color = "blue3") +
annotate("text", label = "New Jersey", x = 2004.5,
y = selected_data[[1,3]] + 1000, color = "red3") +
labs(x = "Year", y = "Average tuition (in USD)",
title = "College Tuition: NY vs NJ") +
xlim(2003.5, 2015.5) + theme(plot.title = element_text(hjust = 0.5)) +
scale_x_continuous(breaks = seq(2004, 2015, by = 1))
animated_plot <- p + transition_reveal(year)
animate(animated_plot, duration = 5, fps = 10)
Lab 2: Try to reproduce the following graph:
Codes:
tuition_data <- read_csv("~/Desktop/us_avg_tuition.csv")
state_data <- tuition_data %>%
pivot_longer(cols = 2:13, names_to = "year",
values_to = "tuition") %>%
mutate(tuition = parse_number(tuition)) %>%
tidyr::extract(year, into = "year", regex = "^(\\d{4})") %>%
mutate(year = as.numeric(year)) %>%
mutate(State = factor(State, levels = sort(unique(State))))
ggplot(state_data,
aes(x = tuition,
y = State,
color = State)) +
geom_col(show.legend = FALSE) +
scale_size_continuous(range = c(0.5, 15), guide = "none") +
labs(title = "Year: {frame_time}", x = "tuition (in USD)", y = NULL) +
xlim(0, 16000) +
theme(plot.title = element_text(size = rel(1.7), hjust = 0.5),
axis.title.y = element_text(size = rel(0.9)),
axis.title.x = element_text(size = rel(1.2))) +
transition_time(year) -> p
animate(p, duration = 4, fps = 3)
Lab 3: 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), color = "blue") +
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, transition_length = 2, state_length = 1) -> p4
animate(p4, fps = 5, res = 150)