Press "Enter" to skip to content

What is leaflet package in R?

Zigya Acadmey 0

What’s leaflet?

The leaflet is an open-source JavaScript library for interactive web maps. The leaflet is designed with simplicityperformance, and usability in mind. It’s lightweightsimple, and flexible, and is probably the most popular open-source mapping library at the moment.

Interaction Features

The leaflet has many features that we can easily use on our project or research as follow

  • Drag panning with inertia
  • Scroll wheel zoom
  • Pinch-zoom on mobile
  • Double click zoom
  • Zoom to the area (shift-drag)
  • Keyboard navigation
  • Events: click, mouseover, etc.
  • Marker dragging

Prerequisites

Before we begin to build the visualization, make sure we fulfil the following requirements:

  • Programming language of R with several libraries, such as leafletleaflet.extras, and dplyr
  • The data consists of location point (latitude and longitude) and additional information.
  • Good internet connection.

Install Packages

We run these lines of code in the console of R Studio to install the required packages.

install.packages("tidyverse")
install.packages("leaflet")

Load Data

Next we read in our data with the read_csv() function from the tidyverse. You may need to alter the file path of your csv in the read_csv() function.

library("tidyverse")
library("leaflet")

Basic Usage

You create a Leaflet map with these basic steps:

  1. Create a map widget by calling leaflet().
  2. Add layers (i.e., features) to the map by using layer functions (e.g. addTilesaddMarkersaddPolygons) to modify the map widget.
  3. Repeat step 2 as desired.
  4. Print the map widget to display it.

Here’s a basic example:

library(leaflet)

m <- leaflet() %>%
# Add default OpenStreetMap map tiles
  addTiles() %>%  
  addMarkers(lng=77.231299, lat=28.613869, popup="Hello World")
# Print the map
m  

Markers

Use markers to call out points on the map. Marker locations are expressed in latitude/longitude coordinates, and can either appear as icons or as circles.

Circle Markers

Circle markers are much like regular circles, except that their radius in onscreen pixels stays constant regardless of zoom level.

You can use their default appearance:

library(leaflet)

m <- leaflet() %>%
# Add default OpenStreetMap map tiles
  addTiles() %>%  
  addCircleMarkers(lng=77.231299, lat=28.613869, popup="Hello World")
# Print the map
m  


Conclusion

Hence, we saw what is leaflet, how to install and load leaflet in R, how to use leaflet, and also how to make plots in the leaflet.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published. Required fields are marked *