Load essential Libraries

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(rgdal)
## Warning: package 'rgdal' was built under R version 3.6.3
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.6.3
## rgdal: version: 1.4-8, (SVN revision 845)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/jcsur/OneDrive/Documents/R/win-library/3.6/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/jcsur/OneDrive/Documents/R/win-library/3.6/rgdal/proj
##  Linking to sp version: 1.4-1
library(countrycode)
library(ggmap)
## Warning: package 'ggmap' was built under R version 3.6.3
## 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)
## Warning: package 'readxl' was built under R version 3.6.3
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.6.3
library(DT)
## Warning: package 'DT' was built under R version 3.6.3
library(taucharts)
library(sp)

1 Create An interactive table of the top 100 cities in the world with the city with the highest PM 10 levels on top.

cities = read_excel('C:\\Users\\jcsur\\OneDrive\\Documents\\DataVis\\OAP_database (1).xls', sheet =4)
## New names:
## * `` -> ...2
## * `` -> ...3
## * `` -> ...4
## * `` -> ...5
## * `` -> ...6
## * ... and 2 more problems
# Eliminating rows and keeping 3 columns
cities <- cities[-c(1:2),c(3,2,4)]

# Renaming 3 columns
names(cities) <- c("City","Country","PMLevel")

str(cities)
## Classes 'tbl_df', 'tbl' and 'data.frame':    1099 obs. of  3 variables:
##  $ City   : chr  "Alger" "Buenos Aires" "Adelaide" "Brisbane" ...
##  $ Country: chr  "Algeria" "Argentina" "Australia" "Australia" ...
##  $ PMLevel: chr  "42" "38" "13.5" "18.166666666666668" ...
# Change PM level from character to number
cities$PMLevel <- round(as.numeric(cities$PMLevel),2)

Interactive table of top 100 cities with the worst PM levels

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

2 Create An interactive bar chart that shows the countries and the number of top 100 cities that are present in each country. The country with the highest number of cities should appear first.

tmp <- cities %>%
  arrange(-PMLevel) %>%
  top_n(100) %>%
  group_by(Country) %>%
  summarise(number_of_cities = length(Country)) %>%
  arrange(-number_of_cities)
## Selecting by PMLevel

Graph to show how many top 100 cities are present in each country

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

## 3. Plot the top 100 cities in a static world map. (Size the points as a function of the amount of PM 10 present in each city.)

cities100 <- cities%>%arrange(-PMLevel) %>%top_n(100)
## Selecting by PMLevel
cities100$CityCountry <- paste(cities100$City, cities100$Country, sep=",")


head(cities100)
## # 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 = "AIzaSyCMzHXS-OjZrst9mwdXFk51pRrlyShGGbs")
# Get Geocodes from Google API
locs <- geocode(cities100$CityCountry)
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Ahwaz,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Ulaanbaatar,Mongolia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Sanandaj,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Ludhiana,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Quetta,Pakistan&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kermanshah,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Peshawar,Pakistan&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Gaborone,Botswana&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Yasouj,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kanpur,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Lahore,Pakistan&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Delhi,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Karachi,Pakistan&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Islamabad,Pakistan&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Lucknow,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Rawalpindi,Pakistan&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Uromiyeh,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Qom,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Indore,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=khoramabad,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Agra,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Al+Ain,United+Arab+Emirates&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Riyadh,Saudi+Arabia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Al-Hafouf,Saudi+Arabia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Lanzhou,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kolkata,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Van,Turkey&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Dakar,Senegal&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Makkah,Saudi+Arabia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Xining,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Urumqi,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Faridabad,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Greater+Cairo,Egypt&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## "Greater Cairo,Egypt" not uniquely geocoded, using "17 mohammed mazhar, zamalek, giza governorate, egypt"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Mexicali,Mexico&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Jabalpur,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Dhaka,Bangladesh&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Mumbai,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Dhanbad,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Ilam,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Jeddah,Saudi+Arabia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Allahabad,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Bushehr,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kerman,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Jinan,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kuwait+City,Kuwait&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Lagos,Nigeria&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Beijing,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Dammam,Saudi+Arabia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Patna,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Sarajevo,Bosnia+and+Herzegovina&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Abu+Dhabi,United+Arab+Emirates&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Meerut,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Xi'an,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Jaipur,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Qazvin,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Medan,Indonesia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Chengdu,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Hefei,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Shenyang,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Yanbu,Saudi+Arabia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kathmandu+Valley,Nepal&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Taiyuan,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Varanasi,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Chongqing,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Wuhan,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Esfahan,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Shijiazhuang,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Konya,Turkey&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kars,Turkey&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Hamedan,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Arak,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Harbin,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Tianjin,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Nanjing,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Denizli,Turkey&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Zhengzhou,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Pune,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Hatay,Turkey&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Nagpur,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Accra,Ghana&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Hangzhou,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Tehran,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Yangon,Myanmar&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Erzurum,Turkey&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Bhopal,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Changsha,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Vijayawada,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Tecate,Mexico&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Yinchuan,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Bangalore,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Rajkot,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Mandalay,Myanmar&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Hyderabad,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Visakhapatnam,India&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Mashhad,Iran+(Islamic+Republic+of)&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Sfax,Tunisia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Tunis,Tunisia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Bizerte,Tunisia&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Changchun,China&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Balikesir,Turkey&key=xxx-OjZrst9mwdXFk51pRrlyShGGbs
# Use ggplot2 for world map
worldmap <- borders("world", fill="lightgray", colour = "white")
worldmap <- ggplot() + worldmap
# Layer cities on top of map
cities100$lat <-locs$lat
cities100$lon <-locs$lon

worldmap <- worldmap + geom_point(data = cities100, aes(x=lon, y=lat, size=PMLevel),alpha=.4,color="red")
worldmap + theme_void()

## 4. Plot the top 100 cities in an interactive world map that shows a popup when a city is clicked. The popup should show information on the name of the city, the country of the city, and the annual mean PM 10 level in each city.

cities100$popup <- paste("<table><tr><td>City:", cities100$City, "<br>Country:", cities100$Country, "<br>Annual Mean PM10 Level:", cities100$PMLevel, "</td></tr></table>")

leaflet(cities100) %>%
  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

## 5. Using the data for different countries from sheet 5, create an interactive choropleth of the world which colors different countries based on the PM 10 levels. Yet again, the popup when a country is clicked should provide information on the name of the country and the PM10 levels.

countries = read_excel('C:\\Users\\jcsur\\OneDrive\\Documents\\DataVis\\OAP_database (1).xls', sheet =5)
## New names:
## * `` -> ...2
## * `` -> ...3
## * `` -> ...4
## * `` -> ...5
## * `` -> ...6
## * ... and 13 more problems
countries <- countries[-c(1:2), c(2:3)]

# Renames the 3 columns
names(countries) <- c("Country", "PMLevel")

str(countries)
## Classes 'tbl_df', 'tbl' and 'data.frame':    91 obs. of  2 variables:
##  $ Country: chr  "Estonia" "Mauritius" "Australia" "New Zealand" ...
##  $ PMLevel: chr  "11.132999999999999" "11.65" "13.179790073011779" "15" ...
countries$iso3c <- as.factor(countrycode(countries$Country, "country.name","iso3c"))

countries$PMLevel <- round(as.numeric(countries$PMLevel),2)

# Preview the data
head(countries)
## # A tibble: 6 x 3
##   Country     PMLevel iso3c
##   <chr>         <dbl> <fct>
## 1 Estonia        11.1 EST  
## 2 Mauritius      11.6 MUS  
## 3 Australia      13.2 AUS  
## 4 New Zealand    15   NZL  
## 5 Ireland        15.2 IRL  
## 6 Luxembourg     17.5 LUX
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, countries,
                   by.x = "ISO_A3",
                   by.y = "iso3c",
                   sort = FALSE, duplicateGeoms = TRUE)
# Using color palette for data
pal <- colorNumeric(
  palette = "Reds",
  domain = countries$PMLevel)
# Displaying a pop up for countries when clicked on:

world_popup <- paste(world$ADMIN, ",PM10 Level:",world$PMLevel, sep = "")

# Adding the map
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)