library(tidyverse)
library(gganimate)
library(gifski)
tuition_data <- read_csv("~/Downloads/us_avg_tuition.csv")
ny_nj_data <- tuition_data %>%
filter(State %in% c("New York", "New Jersey")) %>%
pivot_longer(
cols = -State,
names_to = "year",
values_to = "tuition"
) %>%
mutate(
year = parse_number(year),
tuition = parse_number(as.character(tuition))
) %>%
filter(!is.na(year), !is.na(tuition))
print(ny_nj_data)
## # A tibble: 24 × 3
## State year tuition
## <chr> <dbl> <dbl>
## 1 New Jersey 2004 10054
## 2 New Jersey 2005 10505
## 3 New Jersey 2006 10943
## 4 New Jersey 2007 11427
## 5 New Jersey 2008 11661
## 6 New Jersey 2009 12338
## 7 New Jersey 2010 12752
## 8 New Jersey 2011 12708
## 9 New Jersey 2012 12935
## 10 New Jersey 2013 12998
## # ℹ 14 more rows
p <- ggplot(ny_nj_data,
aes(x = year, y = tuition,
color = State, group = State)) +
geom_line(linewidth = 1) +
geom_point(size = 2) +
geom_text(
data = ny_nj_data %>% group_by(State) %>% slice(1),
aes(label = State),
hjust = -0.1,
show.legend = FALSE
) +
labs(
x = "Year",
y = "Average tuition (USD)",
title = "College Tuition in New York and New Jersey"
) +
scale_x_continuous(breaks = 2004:2015) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5)) +
transition_reveal(year)
animate(p, renderer = gifski_renderer())

library(tidyverse)
library(gganimate)
library(gifski)
diamonds_premium <- diamonds %>%
filter(cut == "Premium")
p <- ggplot(diamonds_premium,
aes(x = carat, y = price, color = color)) +
geom_point(alpha = 0.7, size = 1.3) +
geom_smooth(
aes(group = 1),
method = "loess",
color = "blue",
linewidth = 1.2
) +
labs(
title = "Cut Quality: Premium | Color: {closest_state}",
x = "Carat",
y = "Price (USD)",
color = "color"
) +
coord_cartesian(xlim = c(0, 5), ylim = c(0, 22000)) +
transition_states(color, transition_length = 2, state_length = 1) +
ease_aes("cubic-in-out")
animate(p, renderer = gifski_renderer())
