library(taucharts)
library(DT)
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.4-8, (SVN revision 845)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.4.2, released 2019/06/28
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rgdal/gdal
##  GDAL binary built with GEOS: FALSE 
##  Loaded PROJ.4 runtime: Rel. 5.2.0, September 15th, 2018, [PJ_VERSION: 520]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rgdal/proj
##  Linking to sp version: 1.3-2
library(sp)
library(ggmap)
## Loading required package: ggplot2
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(readxl)
library(leaflet)
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(countrycode)

Reading The File

read_xls("/users/justinmarchiano/downloads/OAP_database.xls")
## New names:
## * `` -> ...2
## # A tibble: 45 x 2
##    `Status 4-10-2011`                               ...2                        
##    <chr>                                            <chr>                       
##  1  <NA>                                            <NA>                        
##  2 "A description of methods and sources is provid… <NA>                        
##  3  <NA>                                            <NA>                        
##  4 "Legend:"                                        <NA>                        
##  5 "PM10:"                                          Particulate matter with dia…
##  6 "PM2.5:"                                         Particulate matter with dia…
##  7 "Urban population coverage: Percent of urban in… <NA>                        
##  8 "Afr: Africa; Amr: America; Emr: Eastern Medite… <NA>                        
##  9 "HI: High income; LMI: Low and middle income."   <NA>                        
## 10  <NA>                                            <NA>                        
## # … with 35 more rows

1.

Isolating Sheet Four

CITY = read_xls("/users/justinmarchiano/downloads/OAP_database.xls", sheet = 4)
## New names:
## * `` -> ...2
## * `` -> ...3
## * `` -> ...4
## * `` -> ...5
## * `` -> ...6
## * ...

Eliminating Rows

CITY <- CITY[-c(1:2),c(3,2,4)]

Renaming Columns and Changing Variables

names(CITY) <- c("city","country","pmlevel")
CITY$pmlevel <- round(as.numeric(CITY$pmlevel),2)

Previewing Data

head(CITY)
## # A tibble: 6 x 3
##   city         country   pmlevel
##   <chr>        <chr>       <dbl>
## 1 Alger        Algeria      42  
## 2 Buenos Aires Argentina    38  
## 3 Adelaide     Australia    13.5
## 4 Brisbane     Australia    18.2
## 5 Bunbury      Australia    13.8
## 6 Canberra     Australia    10.3

Interactive table of the top 100 cities in the world with the city with the highest PM 10 levels on top.

CITY %>%
  arrange(-pmlevel) %>%
  top_n(100) %>%
  datatable(CITY, rownames = FALSE)
## Selecting by pmlevel

2.

Finding Number of Cities

ncit <- CITY %>%
  arrange(-pmlevel) %>%
  top_n(100) %>%
  group_by(country) %>%
  summarise(number_of_CITY = length(country)) %>%
  arrange(-number_of_CITY)
## Selecting by pmlevel

Interactive bar chart that shows the countries and the number of top 100 cities that are present in each country.

ncit$country <- forcats:: fct_inorder(ncit$country)
tauchart(ncit) %>%
  tau_bar("number_of_CITY","country", horizontal = "TRUE") %>%
  tau_legend() %>%
  tau_tooltip()
## Neither color nor size aesthetics have been mapped. Legend plugin will be active but not displayed.

3.

CITY100 <- CITY %>%arrange(-pmlevel) %>%top_n(100)
## Selecting by pmlevel

Seperating City and Country with a Comma

CITY100$citycountry <- paste(CITY100$city, CITY100$country, sep=",")
head(CITY100)
## # A tibble: 6 x 4
##   city        country                  pmlevel citycountry                      
##   <chr>       <chr>                      <dbl> <chr>                            
## 1 Ahwaz       Iran (Islamic Republic …    372  Ahwaz,Iran (Islamic Republic of) 
## 2 Ulaanbaatar Mongolia                    279  Ulaanbaatar,Mongolia             
## 3 Sanandaj    Iran (Islamic Republic …    254  Sanandaj,Iran (Islamic Republic …
## 4 Ludhiana    India                       251  Ludhiana,India                   
## 5 Quetta      Pakistan                    251. Quetta,Pakistan                  
## 6 Kermanshah  Iran (Islamic Republic …    229  Kermanshah,Iran (Islamic Republi…
register_google(key = "AIzaSyBpWZeB28DthS4NFQUgH5Mwfci2tcHVsXc")
locs <- geocode(CITY100$citycountry)
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Ahwaz,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Ahwaz,Iran (Islam..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Ulaanbaatar,Mongolia&key=xxx
## Warning: Geocoding "Ulaanbaatar,Mongolia" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Sanandaj,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Sanandaj,Iran (Is..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Ludhiana,India&key=xxx
## Warning: Geocoding "Ludhiana,India" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Quetta,Pakistan&key=xxx
## Warning: Geocoding "Quetta,Pakistan" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kermanshah,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Kermanshah,Iran (..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Peshawar,Pakistan&key=xxx
## Warning: Geocoding "Peshawar,Pakistan" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Gaborone,Botswana&key=xxx
## Warning: Geocoding "Gaborone,Botswana" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Yasouj,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Yasouj,Iran (Isla..." failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kanpur,India&key=xxx
## Warning: Geocoding "Kanpur,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Lahore,Pakistan&key=xxx
## Warning: Geocoding "Lahore,Pakistan" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Delhi,India&key=xxx
## Warning: Geocoding "Delhi,India" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Karachi,Pakistan&key=xxx
## Warning: Geocoding "Karachi,Pakistan" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Islamabad,Pakistan&key=xxx
## Warning: Geocoding "Islamabad,Pakistan" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Lucknow,India&key=xxx
## Warning: Geocoding "Lucknow,India" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Rawalpindi,Pakistan&key=xxx
## Warning: Geocoding "Rawalpindi,Pakistan" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Uromiyeh,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Uromiyeh,Iran (Is..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Qom,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Qom,Iran (Islamic..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Indore,India&key=xxx
## Warning: Geocoding "Indore,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=khoramabad,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "khoramabad,Iran (..." failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Agra,India&key=xxx
## Warning: Geocoding "Agra,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Al+Ain,United+Arab+Emirates&key=xxx
## Warning: Geocoding "Al Ain,United Ara..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Riyadh,Saudi+Arabia&key=xxx
## Warning: Geocoding "Riyadh,Saudi Arabia" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Al-Hafouf,Saudi+Arabia&key=xxx
## Warning: Geocoding "Al-Hafouf,Saudi A..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Lanzhou,China&key=xxx
## Warning: Geocoding "Lanzhou,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kolkata,India&key=xxx
## Warning: Geocoding "Kolkata,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Van,Turkey&key=xxx
## Warning: Geocoding "Van,Turkey" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Dakar,Senegal&key=xxx
## Warning: Geocoding "Dakar,Senegal" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Makkah,Saudi+Arabia&key=xxx
## Warning: Geocoding "Makkah,Saudi Arabia" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Xining,China&key=xxx
## Warning: Geocoding "Xining,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Urumqi,China&key=xxx
## Warning: Geocoding "Urumqi,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Faridabad,India&key=xxx
## Warning: Geocoding "Faridabad,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Greater+Cairo,Egypt&key=xxx
## Warning: Geocoding "Greater Cairo,Egypt" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Mexicali,Mexico&key=xxx
## Warning: Geocoding "Mexicali,Mexico" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Jabalpur,India&key=xxx
## Warning: Geocoding "Jabalpur,India" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Dhaka,Bangladesh&key=xxx
## Warning: Geocoding "Dhaka,Bangladesh" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Mumbai,India&key=xxx
## Warning: Geocoding "Mumbai,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Dhanbad,India&key=xxx
## Warning: Geocoding "Dhanbad,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Ilam,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Ilam,Iran (Islami..." failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Jeddah,Saudi+Arabia&key=xxx
## Warning: Geocoding "Jeddah,Saudi Arabia" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Allahabad,India&key=xxx
## Warning: Geocoding "Allahabad,India" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Bushehr,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Bushehr,Iran (Isl..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kerman,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Kerman,Iran (Isla..." failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Jinan,China&key=xxx
## Warning: Geocoding "Jinan,China" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kuwait+City,Kuwait&key=xxx
## Warning: Geocoding "Kuwait City,Kuwait" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Lagos,Nigeria&key=xxx
## Warning: Geocoding "Lagos,Nigeria" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Beijing,China&key=xxx
## Warning: Geocoding "Beijing,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Dammam,Saudi+Arabia&key=xxx
## Warning: Geocoding "Dammam,Saudi Arabia" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Patna,India&key=xxx
## Warning: Geocoding "Patna,India" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Sarajevo,Bosnia+and+Herzegovina&key=xxx
## Warning: Geocoding "Sarajevo,Bosnia a..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Abu+Dhabi,United+Arab+Emirates&key=xxx
## Warning: Geocoding "Abu Dhabi,United ..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Meerut,India&key=xxx
## Warning: Geocoding "Meerut,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Xi'an,China&key=xxx
## Warning: Geocoding "Xi'an,China" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Jaipur,India&key=xxx
## Warning: Geocoding "Jaipur,India" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Qazvin,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Qazvin,Iran (Isla..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Medan,Indonesia&key=xxx
## Warning: Geocoding "Medan,Indonesia" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Chengdu,China&key=xxx
## Warning: Geocoding "Chengdu,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Hefei,China&key=xxx
## Warning: Geocoding "Hefei,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Shenyang,China&key=xxx
## Warning: Geocoding "Shenyang,China" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Yanbu,Saudi+Arabia&key=xxx
## Warning: Geocoding "Yanbu,Saudi Arabia" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kathmandu+Valley,Nepal&key=xxx
## Warning: Geocoding "Kathmandu Valley,..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Taiyuan,China&key=xxx
## Warning: Geocoding "Taiyuan,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Varanasi,India&key=xxx
## Warning: Geocoding "Varanasi,India" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Chongqing,China&key=xxx
## Warning: Geocoding "Chongqing,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Wuhan,China&key=xxx
## Warning: Geocoding "Wuhan,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Esfahan,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Esfahan,Iran (Isl..." failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Shijiazhuang,China&key=xxx
## Warning: Geocoding "Shijiazhuang,China" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Konya,Turkey&key=xxx
## Warning: Geocoding "Konya,Turkey" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kars,Turkey&key=xxx
## Warning: Geocoding "Kars,Turkey" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Hamedan,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Hamedan,Iran (Isl..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Arak,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Arak,Iran (Islami..." failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Harbin,China&key=xxx
## Warning: Geocoding "Harbin,China" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Tianjin,China&key=xxx
## Warning: Geocoding "Tianjin,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Nanjing,China&key=xxx
## Warning: Geocoding "Nanjing,China" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Denizli,Turkey&key=xxx
## Warning: Geocoding "Denizli,Turkey" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Zhengzhou,China&key=xxx
## Warning: Geocoding "Zhengzhou,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Pune,India&key=xxx
## Warning: Geocoding "Pune,India" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Hatay,Turkey&key=xxx
## Warning: Geocoding "Hatay,Turkey" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Nagpur,India&key=xxx
## Warning: Geocoding "Nagpur,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Accra,Ghana&key=xxx
## Warning: Geocoding "Accra,Ghana" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Hangzhou,China&key=xxx
## Warning: Geocoding "Hangzhou,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Tehran,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Tehran,Iran (Isla..." failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Yangon,Myanmar&key=xxx
## Warning: Geocoding "Yangon,Myanmar" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Erzurum,Turkey&key=xxx
## Warning: Geocoding "Erzurum,Turkey" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Bhopal,India&key=xxx
## Warning: Geocoding "Bhopal,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Changsha,China&key=xxx
## Warning: Geocoding "Changsha,China" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Vijayawada,India&key=xxx
## Warning: Geocoding "Vijayawada,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Tecate,Mexico&key=xxx
## Warning: Geocoding "Tecate,Mexico" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Yinchuan,China&key=xxx
## Warning: Geocoding "Yinchuan,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Bangalore,India&key=xxx
## Warning: Geocoding "Bangalore,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Rajkot,India&key=xxx
## Warning: Geocoding "Rajkot,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Mandalay,Myanmar&key=xxx
## Warning: Geocoding "Mandalay,Myanmar" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Hyderabad,India&key=xxx
## Warning: Geocoding "Hyderabad,India" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Visakhapatnam,India&key=xxx
## Warning: Geocoding "Visakhapatnam,India" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Mashhad,Iran+(Islamic+Republic+of)&key=xxx
## Warning: Geocoding "Mashhad,Iran (Isl..." failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Sfax,Tunisia&key=xxx
## Warning: Geocoding "Sfax,Tunisia" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Tunis,Tunisia&key=xxx
## Warning: Geocoding "Tunis,Tunisia" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Bizerte,Tunisia&key=xxx
## Warning: Geocoding "Bizerte,Tunisia" failed with error:
## The provided API key is expired.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Changchun,China&key=xxx
## Warning: Geocoding "Changchun,China" failed with error:
## This API project is not authorized to use this API.
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Balikesir,Turkey&key=xxx
## Warning: Geocoding "Balikesir,Turkey" failed with error:
## The provided API key is expired.
CITY100$lat <-locs$lat
CITY100$lon <-locs$lon
worldmap <- borders("world", fill="lightgray", colour = "white")
worldmap <- ggplot() + worldmap
worldmap <- worldmap + geom_point(data = CITY100, aes(x=lon, y=lat, size=pmlevel),alpha=.4,color="red")
worldmap + theme_void()
## Warning: Removed 100 rows containing missing values (geom_point).

4.

CITY100$popup <- paste("<table><tr><td>City:", CITY100$city, "<br>country:", CITY100$country, "<br>Annual Mean PM10 Level:", CITY100$pmlevel, "</td></tr></table>")
leaflet(CITY100) %>%
  addProviderTiles("CartoDB.Positron") %>%
  setView(0, 0, zoom = 2) %>%
  addCircles(stroke = FALSE, fillOpacity = .5, color = "red", radius = ~pmlevel*1000, popup = ~popup)
## Assuming "lon" and "lat" are longitude and latitude, respectively
## Warning in validateCoords(lng, lat, funcName): Data contains 100 rows with
## either missing or invalid lat/lon values and will be ignored

5.

COUNTRY <- read_xls("/users/justinmarchiano/downloads/OAP_database.xls", sheet = 5)
## New names:
## * `` -> ...2
## * `` -> ...3
## * `` -> ...4
## * `` -> ...5
## * `` -> ...6
## * ...
COUNTRY <- COUNTRY[-c(1:2), c(2:3)]
names(COUNTRY) <- c("country", "pmlevel")
COUNTRY$PMLevel <- round(as.numeric(COUNTRY$pmlevel),2)
COUNTRY$iso3c <- as.factor(countrycode(COUNTRY$country, "country.name","iso3c"))
url <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip"
folder <- getwd() #set a folder where to download and extract the data
file <- basename(url) 
 download.file(url, file)
 unzip(file, exdir = folder)
#And read it with rgdal library
world <- readOGR(dsn = folder, 
                 layer = "ne_50m_admin_0_countries",
                 encoding = "UTF-8",
                 verbose = FALSE)
world <- sp::merge(world, COUNTRY,
                   by.x = "ISO_A3",
                   by.y = "iso3c",
                   sort = FALSE, duplicateGeoms = TRUE)
pal <- colorNumeric(
  palette = "Blues",
  domain = COUNTRY$PMLevel)
world_popup <- paste(world$ADMIN, ",PM10 Level:",world$PMLevel, sep = "")
leaflet(data = world) %>%
  addTiles() %>%
  setView(0, 0, zoom = 2) %>%
  addPolygons(fillColor = ~pal(world$PMLevel),
              fillOpacity = 1,
              color = "#000000",
              weight = 1,
              label = ~world_popup) %>%
  addLegend("bottomright", pal = pal, values = ~PMLevel, title = "Amount of PM10 Level", opacity =1)