Folks,

Leaflet is one of the most popular open-source JavaScript libraries for interactive maps.

There are many ways to visualize latitude and longitude data on map using R, such as using ggmaps or RgoogleMaps packages. But these packages generate static maps images only. Leaflet allow users to zoom in or zoom out in a very interactive  way.

In this blog we will learn how to create a interactive map using Leaflet in R & also we will learn how to map and style – latitude and longitude data using R & Leaflet package!

Basics of Leaflet in R – bigdataenthusiast.com/2016/12/12/Leaflet.html


R Packages Required:  

install.packages("leaflet")

1) Creating a map using leaflet –

R Code – 

Code 0.JPG

Here leaflet() initializes the leaflet workspace & addTiles() will bring default OpenStreetMap tiles. OpenStreetMap is a free open-source service to create a free editable map of the world.

Output –

R Viewer (Snapshot)- It allow users to zoom in or zoom out in a very interactive way.

out.JPG

I have published this output on RPubs. Click on below link to see interactive output!

Output Link – rpubs.com/BdataEnthusiast/InteractiveMap

2) Creating a map with single marker –

Suppose user want to mark 28.61293° N, 72.229564° E “India Gate” co-ordinates on Map.

R Code – 

code-1

Here addMarkers – Add markers to the map eg. geo co-ordinates, Popup, link. etc.

Output –

I have published this output on RPubs. Click on below link to see interactive output!

Link – rpubs.com/BdataEnthusiast/InteractiveMap01

Output GIF –

ezgif.com-crop.gif

2) Creating a map with multiple marker –

Suppose user want to mark multiple co-ordinates on Map.

Eg. – Below is R dataframe (india_smart_cities) having  latitude & longitude of 30 proposed smart cities in India.

data.jpg

R Code – 

code multiple.JPG

Output– Click on below link to see interactive output!

Link – rpubs.com/BdataEnthusiast/IndiaSmartCities


If you also share your interactive map outside of the RStudio environment, just click on the Save as Web Page option in Export. It will generate an HTML file.

2


 

Check out this awesome leaflet R Shiny App Blog. This basic R Shiny App allows you to locate your geographic coordinates on Leaflet interactive map.

Check out the Leaflet R Shiny App here shinyapps.io/LeafletShinyR/

leafletshinyr

For more details of R leaflet package, please visit this Link.


Thanks!

Happy Learning! Your feedback would be appreciated!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s