Mapping Polygons

Interactive Maps with leaflet in R

Rich Majerus

Vice President of Strategy & Planning, Queens University of Charlotte

Plotting Polygons

shp %>% 
  leaflet() %>% 
  addTiles() %>% 
  addPolygons()

All NC zips 1

Interactive Maps with leaflet in R

addPolygons()

  • weight - the thickness of the boundary lines in pixels
  • color - the color of the polygons
  • label - the information to appear on hover
  • highlight - options to highlight a polygon on hover
shp %>% 
leaflet() %>% 
addTiles() %>% 
addPolygons(weight = 1,
            color = "grey",
            label = ~paste0("Total Income: " dollar(income)),
            highlight = highlightOptions(weight = 3, color = "red",
                                         bringToFront = TRUE))
Interactive Maps with leaflet in R

addPolygons()

All NC zips 2

Interactive Maps with leaflet in R

Coloring Numeric Data

  • colorNumeric

    nc_pal <- colorNumeric(palette = "Blues", 
                           domain = high_inc@data$mean_income)
    
  • colorBin

    nc_pal <- colorBin(palette = "YlGn", bins = 5,
                       domain = high_inc@data$mean_income)
    
  • colorQuantile

    nc_pal <- colorQuantile(palette = "YlGn", n = 4, 
                            domain = high_inc@data$mean_income)
    
Interactive Maps with leaflet in R

colorNumeric()

nc_pal <- colorNumeric("Blues", domain = high_inc@data$mean_income)

previewColors(pal = nc_pal, values = c(seq(100000, 600000, by = 100000)))

previewColors()

Interactive Maps with leaflet in R

Choropleth Map

nc_pal <- colorNumeric(palette = "Blues", 
                       domain = shp@data$mean_income)

shp %>% leaflet() %>% addTiles() %>% addPolygons(weight = 1, fillOpacity = 1, color = ~nc_pal(mean_income), label = ~paste0("Mean Income: ", dollar(mean_income)), highlight = highlightOptions(weight = 3, color = "red", bringToFront = TRUE))
Interactive Maps with leaflet in R

Choropleth Map

NC income choropleth

Interactive Maps with leaflet in R

Choropleth Example

ggplot(shp@data,
       aes(mean_income)) +
  geom_histogram()

Income histogram

ggplot(shp@data, 
        aes(log(mean_income))) +
  geom_histogram()

Logged income histogram

Interactive Maps with leaflet in R

Logging

NC logged income choropleth 1

Interactive Maps with leaflet in R

Let's practice!

Interactive Maps with leaflet in R

Preparing Video For Download...