get_data <- function(abbrev){
#Get all the cities names for that continent
response <- GET(paste0("https://api.teleport.org/api/continents/geonames:", abbrev, "/urban_areas/"))
content <- fromJSON(rawToChar(response$content))
#Extract the slug:city_name from the url for each city
codes <- content$`_links`$`ua:items`$href %>%
str_extract("slug:[:graph:]*(?=/)")
#Call the urban area API for each city
urban.response <- map(1:length(codes),
~GET(paste0("https://api.teleport.org/api/urban_areas/",
codes[.x], "/details/")))
urban.content <- map(1:length(codes), ~fromJSON(rawToChar(urban.response[[.x]]$content)))
#Convert the list of dataframes into a dataframe
urban.cleaned <- bind_rows(map(1:length(codes), ~urban.content[[.x]]$categories), .id = "Row")
#Filter for data points being looked at
urban.data <- urban.cleaned %>%
filter(label == "Cost of Living" | label == "Outdoors")
#Since the dataframe is made of dataframes create a list for each variable to graph
outdoors <- map(seq(2,2*length(codes),2), ~urban.data[[2]][[.x]]$float_value[1])
cost <- map(seq(1,2*length(codes),2), ~urban.data[[2]][[.x]]$currency_dollar_value[11])
#Combine the lists into a dataframe
country <- do.call(rbind, Map(data.frame, Cost = cost, Elevation = outdoors)) %>%
mutate(Country = abbrev)
return(country)
}
Abbreviations <- c("AF", "AS", "EU", "OC", "SA", "NA")
#Call the function
data <- map(Abbreviations, ~get_data(.x)) %>%
bind_rows() %>%
#Rename the abbreviations with easier to read names
mutate(Country = case_when(Country == "AF" ~ "Africa",
Country == "AS" ~ "Asia",
Country == "EU" ~ "Europe",
Country == "OC" ~ "Oceania",
Country == "SA" ~ "South America",
Country == "NA" ~ "North America"
))
plot <- data %>%
ggplot(aes(x = Cost, y = Elevation, color = Country)) +
geom_point() +
transition_states(Country) +
ggtitle("Elevation by Cost of Resturant Meal for Cities",
subtitle = "Showing the Continent: {closest_state}") +
xlab("Cost of a Resturant Meal(Currency Dollar)") +
ylab("Elevation(meters)") +
shadow_mark(alpha = .3)
animate(plot, nframes = 30, fps = 6)
The goal of this data analysis was to look at the cost of a meal at a resturant and see how it compared to the elevation of that city. I theorized that maybe places with higher elevation would cost more for food. From the graph there doesn’t seem to be any relationship between the cost of a meal at a resturant and the cities elevation. That being said all cities with a high elevation have a lower than average price of a meal at a resturant which conflicts my theory.