rm(list =ls())
library(httr)
library(jsonlite)
library(readxl)
library(irr)
## Loading required package: lpSolve
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(compare)
## 
## Attaching package: 'compare'
## The following object is masked from 'package:base':
## 
##     isTRUE
library(arsenal)
library(anytime)
library(readr)
library(ggplot2)
library(ggpubr)
## Loading required package: magrittr
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:tidyr':
## 
##     extract
library(leaflet)
library(patchwork)
res1 <- GET("https://api.teleport.org/api/urban_areas/slug:new-york/scores/")
res2 <-GET("https://api.teleport.org/api/urban_areas/slug:los-angeles/scores/")
res3 <-GET("https://api.teleport.org/api/urban_areas/slug:san-diego/scores/")
res4 <-GET("https://api.teleport.org/api/urban_areas/slug:chicago/scores/")
res5 <-GET("https://api.teleport.org/api/urban_areas/slug:houston/scores/")
res6 <-GET("https://api.teleport.org/api/urban_areas/slug:phoenix/scores/")
res7 <-GET("https://api.teleport.org/api/urban_areas/slug:philadelphia/scores/")
res8 <-GET("https://api.teleport.org/api/urban_areas/slug:san-antonio/scores/")
res9 <-GET("https://api.teleport.org/api/urban_areas/slug:dallas/scores/")
res10<-GET("https://api.teleport.org/api/urban_areas/slug:san-jose/scores/")
ny<- fromJSON(rawToChar(res1$content))
la<- fromJSON(rawToChar(res2$content))
sd<- fromJSON(rawToChar(res3$content))
chicago<- fromJSON(rawToChar(res4$content))
houston <- fromJSON(rawToChar(res5$content))
phx <- fromJSON(rawToChar(res6$content))
philly<- fromJSON(rawToChar(res7$content))
sa<- fromJSON(rawToChar(res8$content))
dallas <- fromJSON(rawToChar(res9$content))
sj<- fromJSON(rawToChar(res10$content))
ny <- ny$categories
la <- la$categories
sd <- sd$categories
chicago <- chicago$categories
houston <- houston$categories
phx <- phx$categories
philly <- philly$categories
sa <- sa$categories
dallas <- dallas$categories
sj <- sj$categories
ny <- cbind(ny, city = rep("New York", times = 17))
la <- cbind(la, city = rep("Los Angeles", times = 17))
sd <- cbind(sd, city = rep("San Diego", times = 17))
chicago <- cbind(chicago, city = rep("Chicago", times = 17))
houston <- cbind(houston, city = rep("Houston", times = 17))
phx <- cbind(phx, city = rep("Phoenix", times = 17))
philly <- cbind(philly, city = rep("Philadelphia", times = 17))
sa <- cbind(sa, city = rep("San Antonio", times = 17))
dallas <- cbind(dallas, city = rep("Dallas", times = 17))
sj <- cbind(sj, city = rep("San Jose", times = 17))
mostpop <- rbind(ny, la, sd, chicago, houston, phx, philly, sa, dallas, sj)
mostpop <- mostpop[,-1]
mostpop_COL <- mostpop %>% filter(name == "Cost of Living")
mostpop_Ed <- mostpop %>% filter(name == "Education")
mostpop_Safety<- mostpop %>% filter(name == "Safety")
mostpop_Healthcare<- mostpop %>% filter(name == "Healthcare")
COL <- ggplot(mostpop_COL, aes(x = reorder(city, score_out_of_10), y = score_out_of_10)) + 
  geom_bar(stat = "identity", fill = "blue") + 
  coord_flip() + 
  ylab("Cost of Living Score (Out of 10)")+ 
  xlab("City") + 
  theme(legend.position = "none") + 
  ggtitle("Cost of Living Score in Top Ten Most Populous US Cities\n(Note: Higher Scores Indicate Lower Cost of Living)") + 
  theme(text = element_text(size = 5, face = "bold"))

ED<-ggplot(mostpop_Ed, aes(x = reorder(city, score_out_of_10), y = score_out_of_10)) + 
  geom_bar(stat = "identity", fill = "green") + 
  coord_flip() + 
  ylab("Education Score (Out of 10)")+ 
  xlab("City") + 
  theme(legend.position = "none") + 
  ggtitle("Education Score in Top Ten Most Populous US Cities\n(Note: Higher Scores Indicate Better Education)") + 
  theme(text = element_text(size = 5, face = "bold")) 

SAFETY <- ggplot(mostpop_Safety, aes(x = reorder(city, score_out_of_10), y = score_out_of_10)) + 
  geom_bar(stat = "identity", fill = "red") + 
  coord_flip() + 
  ylab("Safety Score (Out of 10)")+ 
  xlab("City") + 
  theme(legend.position = "none") + 
  ggtitle("Safety Score in Top Ten Most Populous US Cities\n(Note: Higher Scores Indicate Higher Level of Safety)") + 
  theme(text= element_text(size = 5, face = "bold"))

HC<-ggplot(mostpop_Healthcare, aes(x = reorder(city, score_out_of_10), y = score_out_of_10)) + 
  geom_bar(stat = "identity", fill = "pink") + 
  coord_flip() + 
  ylab("Safety Score (Out of 10)" )+ 
  xlab("City") + 
  theme(legend.position = "none") + 
  ggtitle("Healthcare Score in Top Ten Most Populous US Cities\n(Note: Higher Scores Indicate Higher Level of Healthcare)" ) + 
  theme(text = element_text(size = 5, face = "bold"))
COL + ED + SAFETY + HC