Data Visualization with Leaflet Using R: Tutorial for the Beginner | by Audhi Aprilliant | Towards Data Science

1 What’s leaflet?2 Interaction Features3 Prerequisites4 Let’s Create the Data Viz5 Recap6 Conclusion7 References
The leaflet is an open-source library for easily making spatial data visualization. Because it is an open-source library and integrated into any platform and programming language, it currently becomes the most popular map library in the world. Historically, the leaflet is firstly developed by Vladimir Agafonkin in 2011.
Before we begin to build the visualization, make sure we fulfill the following requirements.
Programming language of R with several libraries, such as leaflet, leaflet.extras, and dplyr
The data consists of location points (latitude and longitude) and additional information. You can easily download the sample data here
Good internet connection
Building a script to create a leaflet map in R is quite simple. But, if we want to build an interactive and beautiful map, of course, some script must be added. The following script will create our simple map (actually it’s quite cool). Why? Because we have added some information to our marker, such as location’s name, address, longitude, latitude, supervisor, and student's name, instead of only markers. That information is displayed using a popup after we click the marker.
leaflet(data.location) %>%  addProviderTiles(providers$OpenStreetMap) %>%  addMarkers(lng = ~long,             lat = ~lat,             popup = paste(paste('<b>Office:</b>',                                 data.location$place),                           paste('<b>Address:</b>',                                 data.location$address),                           paste('<b>Lat:</b>',                                 data.location$lat),                           paste('<b>Long:</b>',                                 data.location$long),                           paste('<b>Supervisor:</b>',                                 data.location$supervisor),                           data.location$student1,                           data.location$student2,                           data.location$student3,                           sep = '<br/>'),             label = ~place,             group = 'data.location')
Okay, let’s add some features to our map. After zooming in and out of each location, it’s good to reset our view to default. So, we can add a reset map button. Further, we have a search feature to simplify our searching, just by typing our location’s name and the algorithm will show up relevant results based on our keyword. So, just add the following script at the bottom of the previous script.
addResetMapButton() %>%addSearchFeatures(  targetGroups = 'data.location',  options = searchFeaturesOptions(zoom = 15,                                  openPopup = TRUE,                                  firstTipSubmit = TRUE,                                  autoCollapse = TRUE,                                  hideMarkerOnCollapse = TRUE))
Okay then, we add the measure button and highlight it for general info or title. For the measure button, as its name, it will show us the distance between two points or more. We can estimate the distance between two cities using this feature. If we meet the zigzag route, we can draw a straight line point by point. The unit of measurement must be set depending on our needs, for instance in meters. Lastly, to show up our general information, it’s good to add infobox.
addMeasure(    position = 'bottomleft',    primaryLengthUnit = 'meters',    primaryAreaUnit = 'sqmeters',    activeColor = '#3D535D',    completedColor = '#7D4479') %>%addControl("<P><b>Masterpiece Statistics 53</b><br/>Search for offices/ industries<br/>in Java by name.</P>",           position = 'topright')
To recap our script, we can run the following script properly, and voila, our map is ready to interpret and launch to production.
We can look at our map like the following figure.
And we can try the feature of searching. For instance, according to our previous codes, it will read any input from the data.
So, we can easily use leaflets as our tools to visualize the spatial data. It provides many features, such as searching features, zooming, and others. The leaflet is also easy to deploy into the HTML.
[1] A. Woodruff, R. Mullins, C. Jones. Leaflet: Make a web map! (2014), http://maptimeboston.github.io/leaflet-intro.
[2] V. Agafonkin. Leaflet: An Open-source JavaScript Library for Mobile-friendly Interactive Maps (2019), https://leafletjs.com/index.html.