library(rnaturalearth)
library(wbstats)
library(leaflet)

Interactive world map showing PM2.5 values

map <- ne_countries()
## Warning: The `returnclass` argument of `ne_download()` sp as of rnaturalearth 1.0.0.
## ℹ Please use `sf` objects with {rnaturalearth}, support for Spatial objects
##   (sp) will be removed in a future release of the package.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
names(map)[names(map) == "iso_a3"] <- "ISO3"
names(map)[names(map) == "name"] <- "NAME"

indicators <- wb_search(pattern = "pollution")

# wb_data() function : Download Data from the World Bank API
d <- wb_data(
  indicator = "EN.ATM.PM25.MC.M3", 
  start_date = 2017, 
  end_date = 2017
)

map$PM2.5 <- d[match(map$ISO3, d$iso3c), "EN.ATM.PM25.MC.M3"]

df <- map@data
ISO3 <- df$ISO3
NAME <- df$NAME
PM2.5 <- df$PM2.5$EN.ATM.PM25.MC.M3

#  Leaflet map with the PM2.5 values
pal <- colorBin(
  palette = "viridis", domain = PM2.5,
  bins = seq(0, max(PM2.5, na.rm = TRUE) + 10, by = 10)
)

map$labels <- paste0(
  "<strong> Country: </strong> ",
  map$NAME, "<br/> ",
  "<strong> PM2.5: </strong> ",
  PM2.5, "<br/> "
) %>%
  lapply(htmltools::HTML)

leaflet(map) %>%
  addTiles() %>%
  setView(lng = 0, lat = 30, zoom = 2) %>%
  addPolygons(
    fillColor = pal(PM2.5), 
    color = "white",
    fillOpacity = 0.7,
    label = ~labels,
    highlight = highlightOptions(
      color = "black",
      bringToFront = TRUE
    )
  ) %>%
leaflet::addLegend(
  pal = pal, values = PM2.5,
  opacity = 0.7, title = "PM2.5"
)