Quick Tips For Customizing Your R Leaflet Map

[This article was first published on Kevin Zolea on Kevin Zolea, 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.


Maps are apart of our everyday lives. They can be seen in almost every industry and are useful for numerous different purposes. A lot of the time these maps are static. Sometimes, however, it’s extremely useful to interact with the data on the map. This is where the Leaflet package comes in. This post will go over what the Leaflet package is and provide some useful tips on how to make your new interactive map shine.

What is Leaflet ?

Leaflet is an open-source JavaScript library for mobile-friendly interactive maps. It’s used by a great deal of websites and GIS specialists all over the world! The great thing about Leaflet is that it’s super easy to learn and there’s so many tutorials out there to help you. Luckily, instead of having to learn JavaScript theres’s an R Leaflet package. This R package makes it extremely easy to integrate and control Leaflet maps in R.


Before we jump headfirst into some of the tips, I believe you must go over over some material on the Leaflet package in R. This post assumes you know the basics of Leaflet but just in case someone got here and has no clue what I am talking about here are some resources I found helpful when I first started using the package.

Tip #1: Use the leaflet.extras package to add extra functionality to your map

The leaflet.extras package is a great tool to add some cool features to your map using various Leaflet plugins (Note: Not all the plugins are integrated). I won’t go over all the various plugins, instead, I will show you the functions I use and find most useful.

Reset map’s view to original view

Resetting your map’s view to it’s original view is very simple. It can be done with one function: addResetMapButton


leaflet(options = leafletOptions(minZoom = 7))%>%

Adding GPS location (Note: Open the map in a browser not inside Rstudio! Allow the request for location access)


map <- leaflet() %>% addTiles()

map <- addControlGPS(map, options = gpsOptions(position = "topleft", activate = TRUE, 
                                               autoCenter = TRUE, maxZoom = 10, 
                                               setView = TRUE))

Draw features on Leaflet map and have ability to style


leaflet() %>%
  setView(lng = -74.4 ,lat =40, zoom = 8)%>%
  addTiles() %>%
    editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions()))  %>%
  addLayersControl(overlayGroups = c('draw'), options =
                     layersControlOptions(collapsed=FALSE)) %>%

A very interesting post on using the addDrawToolbar() function to select multiple locations on your leaflet map in shiny can be seen here Selecting Multiple Locations on a Leaflet Map in R Shiny. Check it out!

Tip #2: Have map zoom into polygon once polygon is clicked in shiny

Adding polygons to a leaflet map is a very common thing to do in leaflet. This can be achieved by using the addPolygons() function. By giving the user the ability to zoom into the polygon, once a polygon is clicked, it makes it a lot easier to focus in on a particular area of the map. However, before we can use the addPolygons() function we first need to read in a polygon into R! There are many different ways to read in polygons into R but I find the sf package to be the best. To learn more about the sf package you can go here for an overview. Here is a minimal example of reading in a polygon into R and adding it to a leaflet map.


wma<-st_read(dsn="/Users/kevinzolea/Desktop/Personal_Website/content/post/Leaflet/",layer = "WMAs")


wma<-st_zm(wma,drop = T, what = "ZM")

leaflet() %>%
  setView(lng = -74.4 ,lat =40, zoom = 7)%>%
  addTiles() %>%
## Reading layer `WMAs' from data source `/Users/kevinzolea/Desktop/Personal_Website/content/post/Leaflet' using driver `ESRI Shapefile'
## Simple feature collection with 21 features and 9 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 192080.7 ymin: 7556.864 xmax: 667902.7 ymax: 919466.3
## epsg (SRID):    3424
## proj4string:    +proj=tmerc +lat_0=38.83333333333334 +lon_0=-74.5 +k=0.9999 +x_0=150000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs

To leave a comment for the author, please follow the link and comment on their blog: Kevin Zolea on Kevin Zolea.

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)