Phil
Phil

Reputation: 8127

Matching up data with a list of SpatialPolygonsDataFrame objects to run a choropleth map in leaflet

I'm teaching myself how to use mapping tools in R, and in this case I'm looking to set up a choropleth map of various countries at the respective regional levels based on the results from the mydata data frame below.

Note that I'm actually running this for 30 countries. I'm just restricting to 2 countries here for reproducible purposes. Also, note that the getData function will download from the GADM.org website.

library(sp)
library(raster)
library(tidyverse)
library(leaflet)

France_map <- getData(name = "GADM", country = "FRA", level = 1)
Germany_map <- getData(name = "GADM", country = "DEU", level = 1)

all_maps <- list(France_map, Germany_map)

mydata <- dput(structure(list(cntry = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Germany", "France"), 
  class = "factor"), region_name = structure(1:37, .Label = c("Baden-Württemberg", 
  "Bayern", "Berlin", "Brandenburg", "Bremen", "Hamburg", "Hessen", "Mecklenburg-Vorpommern", 
  "Niedersachsen", "Nordrhein-Westfalen", "Rheinland-Pfalz", "Saarland", "Sachsen", 
  "Sachsen-Anhalt", "Schleswig-Holstein", "Thüringen", "Île-de-France", "Champagne-Ardenne", 
  "Picardie", "Haute-Normandie", "Centre", "Basse-Normandie", "Bourgogne", 
  "Nord-Pas-de-Calais", "Lorraine", "Alsace", "Franche-Comté", "Pays de la Loire", 
  "Bretagne", "Poitou-Charentes", "Aquitaine", "Midi-Pyrénées", "Limousin", 
  "Rhône-Alpes", "Auvergne", "Languedoc-Roussillon", "Provence-Alpes-Côte d'Azur"), 
  class = "factor"), prop = c(0.0771812080536913, 0.0808625336927224, 0.154200131820421, 
  0.143712574850299, 0.125, 0.1875, 0.160220994475138, 0.201612903225806, 
  0.149122807017544, 0.140562248995984, 0.111111111111111, 0.130434782608696, 
  0.162454873646209, 0.141891891891892, 0.164835164835165, 0.0810810810810811, 
  0.320361006883289, 0.173944002182893, 0.267984998166555, 0.145696294975402, 
  0.307927600677265, 0.218154962499145, 0.249630808322935, 0.276741747762545, 
  0.0959323496890737, 0.144213110146746, 0.0929990840802643, 0.190088309954592, 
  0.159029556704208, 0.204186267910012, 0.26199124557682, 0.171309738332821, 
  0.125707503945437, 0.241559205433323, 0.170811734710593, 0.275876128675216, 
  0.205483749159249)), .Names = c("cntry", "region_name", "prop"), class = c("tbl_df", 
  "tbl", "data.frame"), row.names = c(NA, -37L)))

What I would like to do now is to display the results from the prop variable of mydata onto a choropleth map, using something like:

leaflet() %>%
  addPolygons()

My stumbling block is, how do I have R match the results in the prop variable to the corresponding regions from the SPDF files to run the choropleth map?

Upvotes: 1

Views: 313

Answers (1)

jazzurro
jazzurro

Reputation: 23574

I combined Germany_map and France_map so that I can draw polygons at once. Then, I created colors for prop. When you use addPolygons(), you specify fillColor = ~mypal(mydata$prop). The prop values do not have to stay in the polygon data. Likewise, you specify values = mydata$prop in addLegend(). You see the result below as a screen shot.

library(sp)
library(raster)
library(maptools)
library(leaflet)

# Combine German and French polygons
mymap <- rbind(Germany_map, France_map, makeUniqueIDs = TRUE)

### Create colors for prop
mypal <- colorNumeric(palette = "Reds", domain = mydata$prop)


leaflet() %>% 
addProviderTiles("OpenStreetMap.Mapnik") %>%
setView(lat = 49.2402, lng = 6.9969, zoom = 6) %>%
addPolygons(data = mymap,
            stroke = FALSE, smoothFactor = 0.2, fillOpacity = 0.5,
            fillColor = ~mypal(mydata$prop),
            popup = paste("Region: ", mymap$NAME_1, "<br>",
                          "Prop: ", mydata$prop, "<br>")) %>%
addLegend(position = "bottomright", pal = mypal, values = mydata$prop,
          title = "Prop",
          opacity = 0.5)

enter image description here

Upvotes: 3

Related Questions