Maps

Author

Drew Clayson

library(tidyverse)
library(leaflet)
library(wbstats)
#Download data
WBData <- wb_data(country = "countries_only", indicator = c("AG.LND.FRST.ZS", "AG.LND.FRST.K2"))
# Load country data (long/lat)
WBCountries <- wb_countries()
clean_data <- WBData[-c(which(is.na(WBData$AG.LND.FRST.K2) | is.na(WBData$AG.LND.FRST.ZS))),]
wb_dat <- merge(clean_data, y = WBCountries[c("iso2c", "longitude", "latitude")], by = "iso2c", all.x = TRUE)
# Rename Columns
colnames(wb_dat)[5] <- 'ForestedArea'
colnames(wb_dat)[6] <- 'PercentForested'
wb_dat <- wb_dat[-c(which(is.na(wb_dat$longitude) | is.na(wb_dat$latitude))),]

The data used in this assignment come from the World Bank. There are two data sets which are being used. The proportion of a country that is forested, and also the total land area of the forests. Also included with world bank data are things like longitude and latitude information for capitals.

# Function to assign colors
make_color <- function(x) {
  sapply(x$PercentForested, function(y) {
    if(y >= 40) {
      "green"
    } else if(y >= 15) {
      "orange"
    } else {
      "red"
    } })
}

# create icon format
icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'fa',   
  text=round(wb_dat$PercentForested), 
  markerColor = make_color(wb_dat)
)
library(leafpop)
library(ggplot2)
wb_dat2 <- wb_dat %>% 
  group_by(country, iso2c) %>%
  summarize(PercentForested=mean(PercentForested),
            latitude = mean(latitude),
            longitude = mean(longitude))

marked_map <- leaflet(wb_dat2) %>% setView(lng = 0, lat = 0, zoom = 1) %>% addProviderTiles(providers$Esri.WorldImagery) %>%
  addAwesomeMarkers(~longitude, ~latitude, icon = icons)
marked_map

This first map places a single marker at the capital of each country, but I found it to be very difficult to read at smaller zoom levels, also some information seems quite wrong compared to raw data, so it is not enough information. I chose to use satellite images because the subject is forestry so why not?

url <- "https://raw.githubusercontent.com/deldersveld/topojson/master/world-countries.json"
geojson <- jsonlite::fromJSON(url)
library("rgdal")
res <- readOGR(dsn = url)
colnames(wb_dat2)[2] <- 'Alpha.2'
chloro_data <- merge(res, y = wb_dat2[c("Alpha.2","PercentForested")], by = 'Alpha.2', all.x = TRUE)
pal <- colorNumeric(
  palette = "RdYlGn",
  domain = chloro_data$PercentForested)
chloro_map <- leaflet(chloro_data) %>%
  addTiles() %>%
  addPolygons(fillColor = ~pal(PercentForested),
              weight = 2,
              opacity = 1,
              color = "white",
              dashArray = "3",
              fillOpacity = 0.5,
              highlightOptions = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE))
chloro_map

This map appears to be superior to the previous map. Though it does not show explicit numbers on the map, it is a very nice looking example that shows general proportions of forestry in different countries. The country shape data came from https://github.com/deldersveld/topojson/blob/master/world-countries.json. I tried using the non-antarctic version but it wasn’t working for some strange reason.

make_ts_popup <- function(id){
  ggplot(filter(wb_dat, id == country)) +
    geom_line(aes(x=date, y = ForestedArea)) +
    theme_minimal() +
    ylab(expression(paste('Forested Area'," (km"^2,")"))) +
    xlab('Date') +
    ggtitle(paste0(filter(wb_dat, id == country)[1,"country"]))
}
plot_all <- lapply(wb_dat2$country, make_ts_popup)



Marked_Map_GraphPopUp <- leaflet(wb_dat2) %>% setView(lng = 0, lat = 0, zoom = 1) %>% addProviderTiles(providers$Esri.WorldImagery) %>%
  addAwesomeMarkers(~longitude, ~latitude,
                    popup = popupGraph(plot_all, width = 300, height = 200),
                    icon = icons)
Marked_Map_GraphPopUp

This map was similar to the first one, but I decided to include popup graphs with time series that show how the area of forests have changed over time. The most interesting time series are in South America in my opinion. Most countries are heavily deforesting aside from Chile, and Uruguay. By far the most interesting thing about this is that Chile and Uruguay are the most developed nations in South America (they have the highest GDP per Capita) and have a different trend in forestation.

Chloro_map_popup <- leaflet(chloro_data) %>%
  addTiles() %>%
  addPolygons(fillColor = ~pal(PercentForested),
              weight = 2,
              opacity = 1,
              color = "white",
              dashArray = "3",
              fillOpacity = 0.5,
              highlightOptions = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = FALSE)
      ) %>%
  setView(lng = 0, lat = 0, zoom = 1) %>%
  addCircleMarkers(wb_dat2$longitude,
                   wb_dat2$latitude,
                   radius = 1,
                   color = "black",
                   fillOpacity = 0.8,
                   popup = popupGraph(plot_all, width = 300, height = 200))
Chloro_map_popup

I thought that this would be the final map which combines the best elements of the two previous graphs. It has colored polygons for different proportions of land containing forests as well as small circular markers in capitals which can be clicked for the same popup graphs as before. I thought to make the circles the same color change as the rest of the countries but it was hard to find capitals if you don’t know where to look. I initially tried to get it mapped so the capital circles weren’t necessary, but after a few attempts at merging the time series with the spatial data, it just wasn’t loading properly.