Cities <- data_frame(Name = c("San Luis Obispo", "San Diego", "Los Angeles", "New York", "Chicago"))
## Warning: `data_frame()` is deprecated as of tibble 1.1.0.
## Please use `tibble()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
#getting all the data for the Cities 
SLO <- data.frame(
fromJSON(rawToChar(GET(paste("https://api.teleport.org/api/urban_areas/slug:",
tolower(str_replace_all(Cities$Name[1], " ", "-")),"/scores/" ,
sep = ""))$content))$categories) 
SLO%>% 
select(name, score_out_of_10) %>% 
mutate(city = "SLO")


SD <- data.frame(
fromJSON(rawToChar(GET(paste("https://api.teleport.org/api/urban_areas/slug:",
tolower(str_replace_all(Cities$Name[2], " ", "-")),"/scores/" ,
sep = ""))$content))$categories) %>% 
select(name, score_out_of_10) %>% 
mutate(city = "SD")


LA <- data.frame(
fromJSON(rawToChar(GET(paste("https://api.teleport.org/api/urban_areas/slug:",
tolower(str_replace_all(Cities$Name[3], " ", "-")),"/scores/" ,
sep = ""))$content))$categories) %>% 
select(name, score_out_of_10) %>% 
mutate(city = "LA")

NY <- data.frame(
fromJSON(rawToChar(GET(paste("https://api.teleport.org/api/urban_areas/slug:",
tolower(str_replace_all(Cities$Name[4], " ", "-")),"/scores/" ,
sep = ""))$content))$categories) %>% 
select(name, score_out_of_10) %>% 
mutate(city = "NY")

CH <- data.frame(
fromJSON(rawToChar(GET(paste("https://api.teleport.org/api/urban_areas/slug:",
tolower(str_replace_all(Cities$Name[5], " ", "-")),"/scores/" ,
sep = ""))$content))$categories) %>% 
select(name, score_out_of_10) %>% 
mutate(city = "Chicago")
all <- bind_rows(SLO, SD, LA, NY, CH) %>% 
  filter(name == "Housing" | name == "Taxation" | name == "Commute") %>% 
  mutate(city = replace_na(city, "SLO")) %>% 
  select(-color)
#creating animated plot
plot2 <- ggplot(all, aes(x = city, y = score_out_of_10, fill = city)) +   
  geom_col() + 
  ggtitle("Satisfaction Score of { closest_state} in different Urban Areas") + transition_states(all$name) + theme_classic() + theme(legend.position = "none", plot.title.position = "plot") + xlab("City") + ylab("Satisfaction Score (Scale 1-10)") + scale_fill_brewer(palette = "Accent") 

animate(plot2, fps = 5)