Using Leaflet in R – Tutorial

[This article was first published on R on Quasilinear Musings, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Here’s a tutorial on using Leaflet in R. While the leaflet package supports many options, the documentation is not the clearest and I had to do a bit of googling to customise the plot to my liking. This walkthrough documents the key features of the package which I find useful in generating choropleth overlays. Compared to the simple tmap approach documented in the previous post, creating a visualisation using leaflet gives more control over the final outcome. I will be covering two different kinds of display options (highlight on mouse over and on-click) as well as the use of layers. The post is an aggregation of materials from the official Leaflet for R documentation page as well as a few other blogs.

Initialising

I will be using a Singapore dataset consisting of the change in religious beliefs from 2000 to 2015, documented in the previous post. The file religion_map belongs to the SpatialPolygonsDataFrame class (though leaflet is also compatible with other classes of data). I have also used the ms_simplify function from the rmapshaper package to simplify the shapefile for web-plotting purposes.

First, we will need to change the projection of the shape file so that it uses a latitude/longitude system.

proj4string(religion_map)
## [1] "+proj=tmerc +lat_0=1.366666666666667 +lon_0=103.8333333333333 +k=1 +x_0=28001.642 +y_0=38744.572 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
religion_map <- spTransform(religion_map, CRS("+proj=longlat +datum=WGS84"))
proj4string(religion_map)
## [1] "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"

Now we can initialise the leaflet file by choosing a basemap which the polygons would subsequently be projected onto. There are a variety of basemaps and a full range of choices can be seen in the following link.

rawleafletmap <- leaflet() %>% 
  addProviderTiles("CartoDB.Positron",
                   options = tileOptions(minZoom=10, maxZoom=13))

The addPolygons function overlay the base map with our desired shapefile. In order to fill the shapes with the desired colours, we need to pass a vector containing the colour hex codes to the fillColor argument of the function. The colorBin function simplifies this task. In this example, I use the “RdYlGn” palette from RColorBrewer which has a nice divergent scheme that suits the data that is going to be presented. To make it easy to follow through the steps, I shall plot the change in Christianity share from 2000 to 2015 (Christianity_diff) for all the subsequent examples.

bins <- c(-20, -10, -5, 0, 5, 10, 20)
pal <- colorBin("RdYlGn", domain = religion_map$Christianity_diff, bins = bins)
leafletmap <- rawleafletmap %>%
  addPolygons(data=religion_map,
              fillColor = ~pal(Christianity_diff))
# The ~ sign is used to signify that the variable used in the argument comes from the data
leafletmap

To leave a comment for the author, please follow the link and comment on their blog: R on Quasilinear Musings.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)