Quick Introduction to Leaflet Maps in R

R has been an all rounder. Be it the implementation of any esoteric statistical algorithm, any database technology or geo-spatial plotting, R has it all and every day new packages are being written to implement new and exciting functionality in R. In some of the previous posts I’ve talked about how we can produce geo-spatial plots in R using ggplot2() and ggmap(). One of the downsides of using basic ggplot2() along with ggmap() is that you can only get static plots!!! There is no interactivity whatsoever and frankly speaking Google maps don’t look that good, with only vanilla ‘stamen’ and ‘satellite’ view.

We as netizens and mobile based app users are habitual to consuming maps produced via projects such as Map box or OpenStreetMap. And frankly speaking these maps look way better than Google maps.

So, the open source R community came up with “leaflet()” package. This one implements the famous java mapping library of the same name, in R. Now one can add popups and markers on the maps.

Below is the sample implementation of code from leaflet() package. The data was taken from https://data.cityofnewyork.us/Housing-Development/Primary-Residential-Zoning-by-lot/ieyi-rqs and this data is about the primary residential zoning lots in Newyork.

setwd(“/media/ramius/E2A02905A028E1B1/Work/Jigsaw Academy/DataScience”)
library(leaflet)
library(magrittr)
library(rgdal)
library(dplyr)
library(rgeos)
library(maptools)
sf<-readOGR(dsn=“Primary_Residential_Zoning_by_lot”,‘EDC_PrimaryZoningResidential_001_14AUG2009’)

## OGR data source with driver: ESRI Shapefile
## Source: “Primary_Residential_Zoning_by_lot”, layer: “EDC_PrimaryZoningResidential_001_14AUG2009”
## with 5 features
## It has 2 fields

#https://data.cityofnewyork.us/Housing-Development/Primary-Residential-Zoning-by-lot/ieyi-rqsn
class(sf)#The data object is a Polygons object

## [1] “SpatialPolygonsDataFrame”
## attr(,”package”)
## [1] “sp”

head(sf@data)

##       ID Zone
## 0 848374    R
## 1 848375    R
## 2 848376    R
## 3 848377    R
## 4 848378    R

head(sf@bbox) #Notice the bounds of map

##        min     max
## x 913128.9 1067317
## y 120858.3  272823

sf<-spTransform(sf,CRS(“+init=epsg:4238”))
m = leaflet() %>% addTiles()
m = m %>% setView((-74.25544+-73.69992)/2, (40.49817+40.91549)/2, zoom = 10)
m%>%addPolygons(data = sf, color = “red”, weight = 2)%>%addPopups(lng =-74.25544,lat =40.49817,popup = “Popup” )

As can be seen the map view is better than that produced by ggplot2()-ggmap() combo. Also the lines of code required to produce this plot are far less.

Interested in learning about other Analytics and Big Data tools and techniques? Click on our course links and explore more.

Jigsaw’s Data Science with SAS Course – click here.
 Jigsaw’s Data Science with R Course – click here.
Jigsaw’s Big Data Course – click here.

Suggested Read:

The Wonderful World of Geospatial Visualizations Explained in 2 Minutes

How Upshot is Using Interactive Visualizations to Make Online News Fun and Interactive

Related Articles

loader
Please wait while your application is being created.
Request Callback