library(tidyverse)
## -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
## v dplyr     1.1.4     v readr     2.1.6
## v forcats   1.0.1     v stringr   1.6.0
## v ggplot2   4.0.1     v tibble    3.3.1
## v lubridate 1.9.4     v tidyr     1.3.2
## v purrr     1.2.1     
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(gganimate)
## Warning: 程序包'gganimate'是用R版本4.5.3 来建造的
library(gifski)
## Warning: 程序包'gifski'是用R版本4.5.3 来建造的
tuition_data <- read_csv("D:/lilith/us_avg_tuition.csv")
## Rows: 50 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (13): State, 2004-05, 2005-06, 2006-07, 2007-08, 2008-09, 2009-10, 2010-...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i 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))

state_data <- tuition_data %>%
  filter(State %in% c("New York", "New Jersey"))

ggplot(state_data, aes(x = year, y = tuition, color = State, group = State)) + 
  geom_line() + 
  geom_point() +
   annotate(
    "text",
    label = "New York",
    x = 2004.5,
    y = filter(state_data, State == "New York")$tuition[1] + 60
  ) +
 
  annotate(
    "text",
    label = "New Jersey",
    x = 2004.5,
    y = filter(state_data, State == "New Jersey")$tuition[1] - 60
  ) +
  
  labs(x = "Year", y = "Average tuition (in USD)", title = "College Tuition in New York 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)
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?

read_csv("D:/lilith/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-...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i 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))


p2 <-ggplot(tuition_data, aes(x = tuition, y =State , color = State, group = State)) + 
  geom_col() + 
  theme(legend.position = "none")+
  labs(x = "tuition(in USD)", y = "state",title = "Year: {closest_state}") + 
  scale_x_continuous(breaks = seq(0,15000, by = 5000))+
  transition_states(year)
animate(p2)

p3 <- ggplot(diamonds) + 
  
  geom_point(aes(x = carat, y = price,color = color),
             alpha = 0.1,
             size = 0.4
             ) + 
  
  geom_smooth(aes(x = carat, y = price, group = cut),
              se = TRUE,
              color = "blue",
              linewidth = 0.8) +
  
  labs(title = "Cut quality: {closest_state}",
       x = "Carat",
       y = "Price (USD)") +
  
  theme_minimal() +
  theme(plot.title = element_text(size = 12, hjust = 0.5)) +
  theme(
  legend.position = "right",
  legend.key.size = unit(0.4, "cm"),
  legend.text = element_text(size = 6),
  legend.title = element_text(size = 8)
)+
  transition_states(cut)

  animate(p3)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

read_csv("D:/lilith/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-...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i 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))


p4 <-ggplot(tuition_data, aes(x = tuition, y = fct_reorder2(State, year, tuition) , color = State, group = State)) + 
  geom_col() + 
  theme(legend.position = "none")+
  labs(x = "tuition(in USD)", y = "fct_reorder2(State, year, tuition)",title = "Year: {closest_state}") + 
  scale_x_continuous(breaks = seq(0,15000, by = 5000))+
  transition_states(year)
animate(p4)