Category Archives: Interactive maps

australia map

Setting Markers and Pop-ups with Leaflet

In this post, we will learn how to set markers and adjust the zoom of interactive maps created with the leaflet package using R.

Load Packages

We begin by loading the only package of this example, which is the leaflet package.

library(leaflet)

Set Markers

Markers are small icons you can place on a map to indicate a specific location. In the code below, we set a marker to indicate Dallas’ area on the map.

leaflet() %>%
addProviderTiles("CartoDB") %>%
addMarkers(lng = -96.8, lat = 32.78) %>% #add marker
setView(lng = -96.8, lat = 32.78, zoom = 12)
ad

The leaflet() function calls create the map. The addProviderTiles() function is used to pick the type of map to display, which in this case is the CartoDB map. The new function in this example is the addMarkers() function, which is used to place the blue marker at the location specified. Lastly, the setView() function sets the location and zoom of the map.

Of course, it is possible to have more than one marker on a map. The example below places markers on different continents.

# Plot both Texas and Belgium locations
leaflet() %>%
addProviderTiles("CartoDB") %>%
addMarkers(lng = c(-96.8, 4.717863), lat = c(32.78, 50.88136))

One aspect that is confusing about setting multiple markers is that you must place all the longitude values together in one concatenate function and all the latitude values in a different concatenate function, as shown above.

Adding Popups

Pops add text to a marker when the mouse hovers over it. The code below is mostly the same as previously, except the pop-up value is set to display Dallas, TX, and Belgium in the two different markers. Also note that we saved our code in an object called “map.”

#Popups
# Store leaflet hq map in an object called map
map <- leaflet() %>%
addProviderTiles("CartoDB") %>%
addMarkers(lng = c(-96.8, 4.717863), lat = c(32.78, 50.88136),
popup = c("Dallas, TX","Belgium"))
#Print map
map

It’s only possible to hover over one marker at a time, but if you run the code, you can confirm that the marker in Belgium also has a pop-up.

Clearing a Map

For our last trick, we will clear the map. To do this, we need to use the clearMarkers() function to clear any markers and the clearBounds() function to remove any zoom or boundaries we set. Below is the code.

#CLEAR MAP
# Remove markers, reset bounds, and store the updated map in the m object
map_clear <- map %>%
clearMarkers() %>%
clearBounds()
# Print the cleared map
map_clear

Now we can begin again with a clean slate.

Conclusion

With these tools, you can now identify specific locations on a map with markers, add text in pop-ups, and clear a map when necessary. Creating interactive maps is another great skill that can be used in various situations.

assorted map pieces

Interactive Maps with R

In this post, we will look at how to make interactive maps in R using the leaflet package. Interactive maps can be a powerful tool to communicate information.

Packages

We will start by loading the only package needed, leaflet.

# Load the leaflet library
library(leaflet)

For the initial examples, loading only this one package is enough.

Using Different Maps

You can load different types of maps using the addTiles() function. Below, we load the default map.

# Create a leaflet map with default map tile using addTiles()
leaflet() %>%
        addTiles()

It is also possible to load other maps. To do this, you will use the addProviderTiles() function. Inside the parentheses, you must include the name of the map after the provider argument. In the example below, we load the “CartoDB” map.

leaflet() %>% 
        addProviderTiles(provider = "CartoDB")
ad

We can extend what we have done so far by including location and zoom, as we will see.

Latitude, Longitude, and Zoom

It is also possible to set the latitude and longitude of the map along with the zoom level. In the code below, we set the coordinates to show the state of Texas, and we set the zoom to 6 so we can see individual city names using the setView() function.

# Texas on the Map with zoom of 6
leaflet()  %>% 
        addProviderTiles("CartoDB")  %>% 
        setView(lng = -99.9018, lat = 31.9686, zoom = 6)

It is also possible to remove the city names with a slight modification to the code.

# Map with no labels zoom of 6
leaflet() %>% 
        addProviderTiles("CartoDB.PositronNoLabels") %>% 
        setView(lng = -99.9018, lat = 31.9686, zoom = 6)

We change the map to one that states “PositronNoLabels” which removes the city and state names.

Predefined Limits

In this example, we will use several options to prevent the map from moving beyond a certain area and also limit how far you can zoom out. First, we use the options argument and set the leafletOptions() to a minimum zoom of 12, which means you cannot zoom out beyond that; we also allow dragging. Next, we pick our map using addProvidersTiles(). After this, we use the setView() function to set the location and zoom. Lastly, we use the setMaxBounds() function to set the location with a plus or minus of .05. Below is the code, followed by a picture of the map.

#stop map from moving beyond preset range
leaflet(options = leafletOptions(
        # Set minZoom and dragging 
        minZoom = 12, dragging = T))  %>% 
        addProviderTiles("CartoDB")  %>% 
        
        # Set default zoom level 
        setView(lng = -96.8, lat = 32.78, zoom = 14) %>% 
        
        # Set max bounds of map 
        setMaxBounds(lng1 = -96.8 + .05, 
                     lat1 = 32.78 + .05, 
                     lng2 = -96.8 - .05, 
                     lat2 = 32.78 - .05)

The map is set over the Dallas area.

Below is the interactive feature of this same map. Notice how when I drag it too far, it automatically pulls me back. This forces the map to always be within a certain area.

Conclusion

The leaflet package provides unique ways to develop interactive maps. As we continue this journey, we will learn of several powerful tools associated with this package.