Module 25 Interactive maps
Learn how to create interactive, beautiful maps with
leafletis so awesome and valuable in reproducible data science.
If you know about the right packages, making really nice maps in
R is actually pretty easy.
R’s spatial visualization tool set is one of the things that elevates it above the other open-source coding tools out there.
Being able to make a map programmatically is a powerful skill. And we can do it quickly and easily in
R thanks to the package
Let’s load it up:
To practice making a map, let’s focus in on the location of Sewanee: The University of the South, in Tennessee, USA.
To begin a
leaflet map, simply type
leaflet(). This creates a blank canvas.
Now, add the background map. To do so,
leaflet uses “tiles”. Tiles are basically images – little pictures stitched together to give you a sense of a continuous map.
Try zooming into this map to get a sense of how powerful these interactive
leaflet maps can be.
To get the map focused on a specific area of interest, bring in “markers” based on your data:
Ta-dah! There’s your map. Try zooming in & out again.
How about satellite imagery instead?
How about topography instead?
leaflet package has plenty of other tile styles available through the
addProviderTiles() function. To preview all the options, check out this site.
Working with markers in
To change the markers from a “dart” to a circle, use
To add ‘pop up’ information when you click on your marker, use the
Click on the marker and see how it looks! Note that we used a bit of
HTML to make the text look prettier.
Mapping marine areas
leaflet is also useful in mapping marine areas.
For example, map the bathymetry of the northeast Pacific basin:
Or get satellite imagery for an island off the coast of Georgia, USA:
To jumpstart your mapping skills in
R, the second package you need to know about is
tidygeocoder. This package helps you “geocode” a mailing address. Geocoding means providing the latitude and longitude for an address, which allows you to find it on a map.
First, install & load the package:
Second, create a dataframe for the address(es) you wish to map:
Check out the result:
And check out your map:
You can also reverse geocode, i.e., get the mailing address for a lat-long coordinate.
Say these are your coordinates of interest:
Check out the result:
Now, add the mailing address as a
popup in your marker:
Maps, even simple ones, are amazingly effective data visualizations. The density and layers of information contained within a map are incredible!
All of those virtues are compounded with an interactie map. At each zoom level, you gain a new perspective. Like
leaflet maps put the viewer in control of how they explore and understand the data.
1. Create a new
2. Create a code chunk. In this chunk, read in some data on “conflicts”. To read in the data, run the below code.
This data comes from https://ucdp.uu.se/encyclopedia. Take a minute or two to look at the website.
3. Have a look in the data. Which fields are likely to be geographic?
4. Make a simple x-y plot using geographic fields.
5. Create an object named
conflicts_afg. This should be a plot of conflicts in Afghanistan.
6. Color the points by year.
7. Instead of year, color the points by
8. Color the points by date, but make point size reflect
9. Create a leaflet map of conflicts in a country of your choice.
addProviderTiles to make your map a satellite map.
12. Explore other tiles
13. Add pop-ups to your maps by using the
popup argument within
clusterOptions = markerClusterOptions() to make your points clustered.
15. Replace your markers with “circle markers”.
16. Create a shiny app wherein the user selects a country and time frame, and the app shows both (a) an interactive map and (b) a plot of the number of conflicts by year for that country.
- This is a review exercise: apply the
ggplotskills introduced in the previous modules to explore the passenger manifest for the RMS Titanic.
This review exercise is best done immediately after the module on Dataframe wrangling.