Exploring the Leaflet Package

R
Senior Project
Author

Clara Broberg

Published

January 28, 2023

Hi everyone, welcome to my first blog post! This semester I am working on my Senior Project. For my senior project I am working with the Wilford Woodruff Papers Foundation. As a part of my project I am going to be doing a location analysis of places where Wilford Woodruff has been and has talked about in his journals. I will be focusing specifically on countys in the United States for my analysis.

While thinking about how I want to visualize my data, I decided I wanted to make an interactive graph of the United States. This will make it so that the user and look at different counties in the United States and then upon selecting a county, they will be able to see when Wilford Woodruff was there. I have used the Leaflet Package in R before, so I decided that this will be the type of visualization that I make. I have little familiarity with the package, so in this blog post I will be exploring the Leaflet package in R further. This will make it so that I can maximize my knowledge on Leaflet, and so that I can create a more quality visualization for my project.

Getting Started with Leaflet

Before doing anything with Leaflet, I first had to install the package.

library(leaflet)

I learned that leaflet has a lot of different ways at you can interact, and visualize data. Not only can you zoom in on maps, but you can also add many different features, such as markers, tiles, polygons, lines, and popups. These features all create different layers on the map, and as many as desired can be added. With this, you can create maps that are as simple or complex as you wish. Leaflet also has default map tiles from OpenStreetMap which uses open data to create world maps.

I started using the package by just creating a simple graph as shown below. This graph uses the latitude and longitude of Rexburg, Idaho to show a marker at its location. When you click the marker, there is a popup that includes the text, “Rexburg, Idaho”.

m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=-111.792824, lat=43.825386, popup="Rexburg, Idaho")
m  # Print the map

Markers

After creating a simple graph, I wanted to see how I could make it more complex. I wanted to accomplish this first by adding more markers and seeing how they can be utilized within Leaflet. The first graph that I made only shows one marker in one location. You can add multiple markers to different locations in order to pin point different places. The graph that I made below uses a UFO sighting data set from Kaggle. I filtered the data set to just show UFO sightings in Idaho and just displayed the top 40 results. I went a step further and also added labels to all of the markers. The labels all show the day that the UFO sighting was posted to the public.

leaflet(data = ufo2[1:50,]) %>% addTiles() %>%
  addMarkers(~city_longitude, ~city_latitude, label = ~as.character(posted))

I made a similar graph to the one above but used marker clusters instead of regular markers. Marker clusters are good for when you have a larger number of markers and want them to display better. It groups the markers together over a larger area, and then when zoomed in, the marker clusters will separate to their exact location. Instead of having a label to each marker, you can also have a pop-up instead. This will make it so that each marker will have to be clicked, instead of just placing your selector over the marker. This is all shown on the graph below.

leaflet(data = ufo2[1:50,]) %>% addTiles() %>%
  addMarkers(~city_longitude, ~city_latitude, popup = ~as.character(posted), 
             clusterOptions = markerClusterOptions())

Lines and Shapes

Lines can be added to a Leaflet graph in order to outline a country, state, county, etc. They can be helpful when you want a state to stand out or have a different value. You can also have lines be highlighted when they are scrolled over. This allows there to be more interaction with the graph.

Layers

Layers can also be added to any Leaflet graph. Layers are very helpful when you want to view data from different years or from different categories. Layers create a legend on the side that allow you to select what you want to view. This can be helpful for easy comparison or just to view on object at a time.

The graph below uses layers to show 3 of the different shapes of UFO sightings in Idaho, circles, light, and triangle. Using the drop down in the top right corner, the user can change filters based off of what they want to see. The graph also has a pop-up that shows the duration of each UFO sighting.

leaflet(ufo2) %>% addTiles() %>%
  addCircles(data = circle, lng = ~city_longitude, lat = ~city_latitude, weight = 10, color = "dodgerblue", radius = ~city_latitude, popup = ~duration, group = 'Circle') %>%
  addCircles(data = triangle, lng = ~city_longitude, lat = ~city_latitude, weight = 10,
             color = 'deeppink',radius = ~city_latitude, popup = ~duration, group = 'Triangle') %>%
  addCircles(data = light, lng = ~city_longitude, lat = ~city_latitude, weight = 10,
             color = 'goldenrod',radius = ~city_latitude, popup = ~duration, group = 'Light') %>%
  addLayersControl(overlayGroups = c("Circle","Light", "Triangle"))

Final Thoughts

I think that diving more into the Leaflet package overall was very helpful. I think that all of the different features that I learned will be able to help me when making a graph for my senior project.

In my project, I plan on using markers for all of the different locations that are mentioned in the Wilford Woodruff papers, and then having labels that describe what the location is, and possibly what happened there in Wilford’s life. This package was also very helpful because it helped me learn how to create interactive visualizations. I think that I will definetly use this package in the future when I want to create different maps.