library(tidyverse)
library(leaflet)
library(wbstats)
#Download data
<- wb_data(country = "countries_only", indicator = c("AG.LND.FRST.ZS", "AG.LND.FRST.K2"))
WBData # Load country data (long/lat)
<- wb_countries()
WBCountries <- WBData[-c(which(is.na(WBData$AG.LND.FRST.K2) | is.na(WBData$AG.LND.FRST.ZS))),]
clean_data <- merge(clean_data, y = WBCountries[c("iso2c", "longitude", "latitude")], by = "iso2c", all.x = TRUE)
wb_dat # Rename Columns
colnames(wb_dat)[5] <- 'ForestedArea'
colnames(wb_dat)[6] <- 'PercentForested'
<- wb_dat[-c(which(is.na(wb_dat$longitude) | is.na(wb_dat$latitude))),] wb_dat
Maps
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
<- function(x) {
make_color sapply(x$PercentForested, function(y) {
if(y >= 40) {
"green"
else if(y >= 15) {
} "orange"
else {
} "red"
} })
}
# create icon format
<- awesomeIcons(
icons icon = 'ios-close',
iconColor = 'black',
library = 'fa',
text=round(wb_dat$PercentForested),
markerColor = make_color(wb_dat)
)library(leafpop)
library(ggplot2)
<- wb_dat %>%
wb_dat2 group_by(country, iso2c) %>%
summarize(PercentForested=mean(PercentForested),
latitude = mean(latitude),
longitude = mean(longitude))
<- leaflet(wb_dat2) %>% setView(lng = 0, lat = 0, zoom = 1) %>% addProviderTiles(providers$Esri.WorldImagery) %>%
marked_map 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?
<- "https://raw.githubusercontent.com/deldersveld/topojson/master/world-countries.json"
url <- jsonlite::fromJSON(url)
geojson library("rgdal")
<- readOGR(dsn = url)
res colnames(wb_dat2)[2] <- 'Alpha.2'
<- merge(res, y = wb_dat2[c("Alpha.2","PercentForested")], by = 'Alpha.2', all.x = TRUE)
chloro_data <- colorNumeric(
pal palette = "RdYlGn",
domain = chloro_data$PercentForested)
<- leaflet(chloro_data) %>%
chloro_map 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.
<- function(id){
make_ts_popup 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"]))
}<- lapply(wb_dat2$country, make_ts_popup)
plot_all
<- leaflet(wb_dat2) %>% setView(lng = 0, lat = 0, zoom = 1) %>% addProviderTiles(providers$Esri.WorldImagery) %>%
Marked_Map_GraphPopUp 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.
<- leaflet(chloro_data) %>%
Chloro_map_popup 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,
$latitude,
wb_dat2radius = 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.