1. Basics


Load packages and pull data

library(readr)
library(tidyverse)
library(leaflet)
cluster_demog<-read_csv(
"http://ec2-54-235-58-226.compute-1.amazonaws.com/storage/f/2014-02-23T17%3A11%3A51.737Z/dc-neighborhood-cluster-demographics.csv")

Blank Map

leaflet( data = cluster_demog ) %>%
    addTiles() 

Add Markers

Add markers with addMarkers(), specifying the columns for latitude, lat, and longitude, lng, at minimum.

leaflet( data = cluster_demog ) %>%
    
    addTiles() %>%
    
    addMarkers(   lng = cluster_demog$lon_ctr      ### !!! ### 
                , lat = cluster_demog$lat_ctr        ### !!! ### 
                )                                  ### !!! ### 

Change Map “Tiles”

To do this, we swap the default addTiles() for addProviderTiles(providers$name_of_desired_tile). You can preview the built-in Leaflet tiles here.

leaflet( data = cluster_demog ) %>%
    
    addProviderTiles( providers$CartoDB.Positron ) %>%    ### !!! ### 
   
    addMarkers(   lng = cluster_demog$lon_ctr 
                , lat=cluster_demog$lat_ctr 
                )
leaflet( data = cluster_demog ) %>%
    
    addProviderTiles( providers$NASAGIBS.ViirsEarthAtNight2012 ) %>%    ### !!! ### 
    
    addMarkers(   lng = cluster_demog$lon_ctr 
                , lat = cluster_demog$lat_ctr 
                )

Set the initial corrdinates and zoom level

The setView() utility function with three arguments: latitude, lat, and longitude, lng, and the zoom level, zoom.

 ### !!! ### 
leaflet( data = cluster_demog ) %>% 
    setView(lng = -77.0369, lat = 38.9072,  zoom=04) %>%           ### !!! ### 
    
    addProviderTiles( providers$NASAGIBS.ViirsEarthAtNight2012 ) %>%   
    
    addMarkers(   lng = cluster_demog$lon_ctr 
                , lat = cluster_demog$lat_ctr 
                )

2. Pop-ups


Add Pop-ups

To do this, we add popup = desired_popup_data_here as an argument into addMarkers().

leaflet( data = cluster_demog ) %>%
    
    addProviderTiles(providers$CartoDB.Positron) %>%
    
    addMarkers(   lng = cluster_demog$lon_ctr 
                , lat=cluster_demog$lat_ctr 
                , popup =  cluster_demog$Full_Name       ### !!! ###     
                )  

Add More Info to Pop-ups

To do this, we just simply paste() the information together

 leaflet( data = cluster_demog ) %>%
    
    addProviderTiles(providers$CartoDB.Positron) %>%
    
    addMarkers(   lng = cluster_demog$lon_ctr 
                , lat=cluster_demog$lat_ctr 
                , popup =  paste(   cluster_demog$Full_Name                ### !!! ###     
                                  , cluster_demog$neighborhood_cluster ,sep=" "     ### !!! ### 
                                  )                                        ### !!! ### 
                )  

Format Pop-ups

Let’s separate the pop-up details with a line break. To do this, assign the sep argument of paste() to sep = "<br/>"

 leaflet( data = cluster_demog ) %>%
    
    addProviderTiles(providers$CartoDB.Positron) %>%
    
    addMarkers(   lng = cluster_demog$lon_ctr 
                , lat=cluster_demog$lat_ctr 
                , popup =  paste(   sep = "<br/>"                     ### !!! ###     
                                  , cluster_demog$Full_Name          
                                  , cluster_demog$neighborhood_cluster 
                                  
                                  )
                )  

3. Markers


From Plain Markers to Circle Markers

To do this, we will use addCircleMarkers() instead of addMarkers().

leaflet( data = cluster_demog ) %>%
    
    addProviderTiles(providers$CartoDB.Positron) %>%
    
    addCircleMarkers(   lng = cluster_demog$lon_ctr                      ### !!! ###
                , lat=cluster_demog$lat_ctr 
                , popup =  paste(   sep = "<br/>"                         
                                  , cluster_demog$Full_Name          
                                  , cluster_demog$neighborhood_cluster 
                                  
                                  )
                )  

Circle Marker Radius

 leaflet( data = cluster_demog ) %>%
    
    addProviderTiles(providers$CartoDB.Positron) %>%
    
    addCircleMarkers(   lng = cluster_demog$lon_ctr                      ### !!! ###
                , lat=cluster_demog$lat_ctr 
                , popup =  paste(   sep = "<br/>"                         
                                  , cluster_demog$Full_Name          
                                  , cluster_demog$neighborhood_cluster 
                                  )
                , radius = cluster_demog$`Population,_2010`
                )  

Ew. Circles are so large that they fill the map!

Circle Marker Radius

 leaflet( data = cluster_demog ) %>%
    
    addProviderTiles(providers$CartoDB.Positron) %>%
    
    addCircleMarkers(   lng = cluster_demog$lon_ctr                     
                , lat=cluster_demog$lat_ctr 
                , popup =  paste(   sep = "<br/>"                         
                                  , cluster_demog$Full_Name          
                                  , cluster_demog$neighborhood_cluster 
                                  )
                , radius = cluster_demog$`Population,_2010`/5000              ### !!! ###
                )  

4. Choropleths, GeoJSON, Spatial Polygons


Oh My!

You might want this later: https://rstudio.github.io/leaflet/choropleths.html

Using package geojsonio

#library(geojsonio)
cluster_outlines<-geojsonio::geojson_read("http://data.codefordc.org/dataset/8c0a1a57-8abf-4a29-abbb-ee2b890a878e/resource/3f9ce2d9-b438-4dab-a232-694cb80c7964/download/neighborhoodclusters.geojson", what = "sp")

cluster_outlines$Cluster_num<-as.numeric(grep("([0-9]+).*$",  as.character(cluster_outlines$NAME)))
leaflet(cluster_outlines)  %>%
  addTiles() %>% 
    addPolygons()

Color between the lines

Choosing to color by 2010 population.

#summary(cluster_demog$`Population,_2010`)
cluster_outlines@data<-merge(cluster_outlines@data, cluster_demog, by="Cluster_num")

cut_offs<-data.frame(bins=quantile(cluster_outlines$`Population,_2010`, c(0,.2,.3,.4,.5,.6,.7,.8,1)))
bins <-c(0,cut_offs$bins)
pal <- colorBin("YlOrRd", domain = cluster_outlines$`Population,_2010`, bins = bins)



leaflet(cluster_outlines)  %>%
  addTiles() %>% 
    addPolygons(fillColor = ~pal(`Population,_2010`)                 ### !!! ###
     )

Make it easier to see

leaflet(cluster_outlines)  %>%
  addTiles() %>% 
   addPolygons(fillColor = ~pal(`Population,_2010`)  
      ,weight = 2,                                       ### !!! ###
  opacity = 1,                                            ### !!! ###
  color = "white",                                        ### !!! ###
#  dashArray = "3",
  fillOpacity = 0.7)

Another option

leaflet(cluster_outlines)  %>%
  addProviderTiles(providers$CartoDB.Positron) %>%              ### !!! ###
   addPolygons(fillColor = ~pal(`Population,_2010`)  
      ,weight = 1,
  opacity = 1,
  color = "white",
  
 # dashArray = "3",
  fillOpacity = 0.6)

Hover Acation!

leaflet(cluster_outlines)  %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
   addPolygons(fillColor = ~pal(`Population,_2010`)  
      ,weight = 1,
  opacity = 1,
  color = "white",
  fillOpacity = 0.6,
  highlight = highlightOptions(                     ### !!! ###
    weight = 2,                                     ### !!! ###
    fillOpacity = 0.75,                             ### !!! ###
    bringToFront = TRUE)                            ### !!! ###
  )

Pop-ups

labels <- sprintf(
  "<strong>%s</strong><br/>Population: %g",
  cluster_outlines$NBH_NAMES, cluster_outlines$`Population,_2010`
) %>% lapply(htmltools::HTML)



leaflet(cluster_outlines)  %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
   addPolygons(fillColor = ~pal(`Population,_2010`)  
      ,weight = 1,
  opacity = 1,
  color = "white",
  fillOpacity = 0.6,
  
  highlight = highlightOptions(
    weight = 2,
    fillOpacity = 0.75,
    bringToFront = TRUE)
  ,label = labels                                ### !!! ###
  )

If the geojsonio package won’t install

If you can’t get geojsonio to work, you will want to try using the jsonlite package paired with the function readLines(). This example is incomplete, but will at least get you past the first step of reading in the geojson.

library(jsonlite)

geojson <- readLines("http://data.codefordc.org/dataset/8c0a1a57-8abf-4a29-abbb-ee2b890a878e/resource/3f9ce2d9-b438-4dab-a232-694cb80c7964/download/neighborhoodclusters.geojson"
    , warn = FALSE) %>%
  paste(collapse = "\n") %>%
  fromJSON(simplifyVector = FALSE)