Question: 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_data <- filter(tuition_data, State == "New Jersey"|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 = filter(ny_data, State == "New York")$tuition[1] + 60) +
annotate("text", label = "New Jersey", x = 2004.5, y = filter(ny_data, State == "New Jersey")$tuition[1] + 60) +
labs(
x = "Year",
y = "Average tuition (in USD)",
title = "College Tuition in New York and New Jersey"
) +
xlim(2003.5, 2015.5) +
scale_x_continuous(breaks = seq(2004, 2015, by = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
transition_reveal(year)
Question: Try to reproduce the following graph:
gapminder <- read_csv("gapminder.csv")
avg_tuition <- tuition_data %>%
group_by(State, year) %>%
summarise(tuition = mean(tuition, na.rm = TRUE), .groups = "drop")
p <- ggplot(avg_tuition, aes(x = tuition, y = State, color = State)) +
geom_col(show.legend = F) +
scale_size_continuous(range = c(0.5, 15), guide = "none") +
labs(title = 'Year: {frame_time}', x = "Average tuition (in USD)", y = "State") +
transition_time(year) +
ease_aes('linear') +
theme(axis.title = element_text(size = rel(1.2)),
plot.title = element_text(hjust = 0.5),
axis.text = element_text(size = rel(0.6)))
animate(p, duration = 4, fps = 3)
Question: Try to reproduce the following graph with
the diamonds data set:
p1 <- ggplot(diamonds, aes(x = carat, y = price, color = color)) +
geom_point(alpha = 0.7, size = 0.8) +
geom_smooth(method = "loess", color = "blue") +
labs(
title = 'Cut Quality: {closest_state}',
x = 'Carat',
y = 'Price (USD)'
) +
theme(
plot.title = element_text(hjust = 0.5)
) +
transition_states(cut) +
enter_fade() +
exit_disappear() +
ease_aes('sine-in-out')
animate(p1, fps = 10, duration = 8)