Creating Maps with the leaflet Package

Creating Maps

Maps are fantastic and complicated all at the same time! Maps are some of the richest visualizations around because they come with data built into them. That is, on top of whatever data you’re going to plot you are also probably going to view terrain, roads, state/country borders, city names or a host of other information that come with our geography.

This implicit richness only adds to challenge of creating a good map. The XKCD comic on the right displays a great example of how creating maps can be…difficult.

A black-and-white comic shows a presenter pointing at a screen with three U.S. heat maps. The maps are labeled 'Our Site’s Users', 'Subscribers to Martha Stewart Living', and 'Consumers of Furry Pornography'. All three maps look nearly identical, with dense red and yellow areas around major population centers and mostly green elsewhere. The presenter says, 'The business implications are clear.' A caption at the bottom reads: 'Pet Peeve #208: Geographic profile maps which are basically just population maps.'

Required-videoRequired Video

Be sure to watch all 6 videos in this series!

Check-inCheck In
  1. Is running just the leaflet() function enough to produce a map?
  • Yes
  • No
  1. To plot data points on a leaflet map, we should probably use which function?
  • addTiles()
  • addMarkers()
  • addProviderTiles()
  • addPolygons()

More on the leaflet package

The leaflet package is actually extremely rich and can basically enable (interactive) map visualizations as complex as you can imagine.

Required-readingRequired Reading

Take some time to peruse the site for the leaflet R package: https://rstudio.github.io/leaflet/

Check-inCheck In
  1. What do the names of your plotted data columns have to be?
  • x; y
  • leafletx; `leaflety
  • lat / latitude; lon/ longitude
  • Anything as long as you tell the addMarkers() function correctly
  1. If you were to think of latitude and longitude as coordinates in the cartesian coordinate system, then…
  • latitude is on the horizontal axis and longitude is on the vertical
  • latitude is on the vertical axis and longitude is on the horizontal

Practice With Leaflet

helmet_icon <- makeIcon(
  iconUrl = "http://www.clker.com/cliparts/l/R/b/J/X/D/astronaut-helmet-md.png",
  iconWidth = 15, 
  iconHeight = 15
  )

leaflet() %>%
  addTiles() %>%
  addMarkers(lng = jitter(space$Longitude, 
                          factor = 2), 
             lat = jitter(space$Latitude,
                          factor = 2), 
             icon = helmet_icon, 
             popup = paste(space$Name, "<br>", 
                           "Birth Place: ", space$`Birth Place`),
             label = paste(space$Name)
             )
Check-inCheck In

The above map plots data on NASA astronauts!

  1. The graphic for each marker is a little astronaut helmet. How cute! This image was used via…
  • a URL pointing to it on the web.
  • a downloaded image file.
  1. The popup argument specifies what you see when you ______, while the label argument specifies what you see when you ______.
  • hover; click
  • zoom-in; zoom-out
  • click; hover
  • squint your eyes; look normally
Learn-moreLearn More
  • Greg Sward has a pretty good series of videos on leaflet in R - the entire series is longer than we can fit as required viewing, but they are great tutorials!
  • Abhinav Agrawal also has a good beginner series
  • Don’t forget about the Data-to-Viz Website