Load libraries

library(tidyverse)
library(gganimate)
library(gifski)

Lab Exercise 1

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("~/Documents/MasterDS/DAS522_Exploratory_Data_Analysis_and_Visualization/HW3/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 York" | State == "New Jersey")

ggplot(ny_data, aes(x = year, y = tuition, color = State, group = State)) + 
  geom_line() + 
  geom_point() +
  annotate("text", label = "New Jersey", x = 2004.5, y = ny_data[[1,3]] - 60) +
  annotate("text", label = "New York", x = 2004.5, y = ny_data[[13,3]] + 160) +
  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)) +
  transition_reveal(year)

Animate time series data using gapminder data set from World Bank

gapminder <- read_csv("~/Documents/MasterDS/DAS522_Exploratory_Data_Analysis_and_Visualization/datasets/gapminder_DAS522.csv")

ggplot(gapminder, aes(Fertility, LifeExp, size = Pop, colour = region)) +
  geom_point(alpha = 0.7, show.legend = T) +
  scale_size_continuous(range = c(0.5, 15), guide = "none") +
  # Here comes the gganimate specific bits
  labs(title = 'Year: {frame_time}', x = 'Fertility Rate', y = 'life expectancy') +
  xlim(0, 10) + ylim(0, 100) + theme(plot.title = element_text(hjust = 0.5)) + 
  transition_time(date) +
  ease_aes('linear') -> p
animate(p, duration =120, fps =0.5)

Lab Exercise 2

Try to reproduce the following graph.

tuition_data %>%
ggplot(aes(x = tuition, y = State, color = State))+
  geom_bar(stat = "identity")+
  labs(title = 'Year: {frame_time}', x = 'tuition (in USD)') +
  theme(legend.position = "none",plot.title = element_text(hjust = 0.5), axis.text.y = element_text(size = rel(0.8)) ) +
  transition_time(year) +
  ease_aes('linear') -> t
animate(t, duration = 12, fps = 1)

Lab Exercise 3

Try to reproduce the following graph with the diamonds data set:

ggplot(diamonds) +
  geom_point(aes(x = carat, y = price, color = color)) +
  geom_smooth(aes(x = carat, y = price))+
  labs(title = "Cut Quality:{closest_state}", x = "Carat", y = "Price (USD)") +
  theme(plot.title = element_text(hjust = 0.5)) +
  enter_fade() + exit_shrink() +
  transition_states(cut)