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("/Users/HoangDucVinh/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_tidied <- tuition_data %>%
pivot_longer(cols = 2:13, names_to = "year", values_to = "tuition") %>%
mutate(
year = as.numeric(substr(year, 1, 4)),
tuition = as.numeric(gsub("[\\$,]", "", tuition))
)
nynj_data <- tuition_tidied %>%
filter(State %in% c("New York", "New Jersey"))
ggplot(nynj_data, aes(x = year, y = tuition, color = State, group = State)) +
geom_line() +
geom_point() +
annotate("text", label = "New York", x = 2004.5, y = 6200, color = "coral") +
annotate("text", label = "New Jersey", x = 2004.5, y = 10000, color = "steelblue") +
labs(
x = "Year",
y = "Average tuition (in USD)",
title = "College Tuition: New York vs. New Jersey"
) +
scale_x_continuous(breaks = seq(2004, 2015, by = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
transition_reveal(year)
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?