library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(forcats)
library(ggplot2)
library(gganimate)

Plot using fct_reorder2 to allow state reordering over time

read_csv("~/Downloads/us_avg_tuition.csv") -> tuition_data
## Rows: 50 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (13): State, 2004-05, 2005-06, 2006-07, 2007-08, 2008-09, 2009-10, 2010-...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
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))

ggplot(tuition_data, aes(x = tuition,
                         y = fct_reorder2(State, year, tuition),
                         fill = State)) +
  geom_col() +
  scale_x_continuous(limits = c(0, 16000), breaks = seq(0, 15000, by = 5000)) +
  labs(title = 'Year:{closest_state}',
       x = 'tuition (in USD)',
       y = 'fct_reorder2(State, year, tuition)') +
  theme(legend.position = "none",
        plot.title = element_text(hjust = 0.5, size = 18)) +
  transition_states(year, transition_length = 2, state_length = 1) +
  ease_aes('linear')

******Plot using fct_reorder2 to allow state reordering over time***

ggplot(tuition_data, aes(x = tuition,
                         y = fct_reorder2(State, year, tuition),
                         fill = State)) +
  geom_col() +
  scale_x_continuous(limits = c(0, 16000), breaks = seq(0, 15000, by = 5000)) +
  labs(title = 'Year:{closest_state}',
       x = 'tuition (in USD)',
       y = 'fct_reorder2(State, year, tuition)') +
  theme(legend.position = "none",
        plot.title = element_text(hjust = 0.5, size = 18)) +
  transition_states(year, transition_length = 2, state_length = 1) +
  ease_aes('linear')

Plot using diamonds dataset from ggplot2

ggplot(diamonds, aes(x = carat, y = price, color = color)) +
  geom_point(alpha = 0.5) +
  geom_smooth(method = "loess", color = "blue", se = TRUE, linewidth = 1.2) +
  labs(title = 'Cut Quality: {closest_state}',
       x = 'Carat',
       y = 'Price (USD)') +
  theme(
    plot.title = element_text(hjust = 0.5, size = 16)
  ) +
  transition_states(cut, transition_length = 2, state_length = 1) +
  ease_aes("linear")
## `geom_smooth()` using formula = 'y ~ x'