Skip to content

Lab 3F: Maps

Lab 3F - Maps

Directions: Follow along with the slides, completing the questions in blue on your computer, and answering the questions in red in your journal.

Informative and Fun!

  • Maps are some of the most interesting plots to make because the info represents:

    • Where we live.

    • Where we go.

    • Places that interest us.

  • Maps are also helpful to display geographic information.

  • In this lab, we'll use R to create an interactive map of the mtns data we scraped in Lab 3E.

Getting ready to map

  • The map we'll be creating will end up in RStudio's Viewer pane.

    – Which means you'll need to alternate between building the map and loading the lab.

  • You'll find it very helpful, for this lab, to write all of the commands, including the load_lab(23) command, as an R Script.

    – This way you can edit the code that builds the map and quickly reload the lab.

Load your data!

  • In Lab 3E you created a dataset. Load it into Rstudio now by filling in the blank with the file name of the data.

  • Didn't finish the lab or save the data file? Ask a friend to share it!

Build a Basic Map

  • Let's start by building a basic map!

  • Use the leaflet() function and the mtns data to create the leaf that we can use for mapping.

    mtns_leaf <- leaflet(____)
  • Then, insert mtns_leaf into the addTiles() function and assign the output the name mtns_map.

  • Run mtns_map in the console to look at your basic map with no data displayed.

    – Be sure to try clicking on the map to pan and zoom.

Including our data

  • Now we can add markers for the locations of the mountains using the addMarkers() function.

  • Fill in the blanks below with the basic map we've created and the values for latitude and longitude.

    addMarkers(map = ____, lng = ~____, lat = ~____)
  • Supply the peak variable, in a similar way as we supplied the lat and long variables, to the popup argument and include it in the code above.

  • Click on a marker within California and write down the name of the mountain you clicked on.


  • Our current map looks pretty good, but what if we wanted to add some colors to our plot?

  • Fill in the blanks below to create a new variable that assigns a color to each mountain based on the state it's located in.

    mtns <- mutate(____, state_colors = colorize(____))
  • Now that we've added a new variable, we need to re-build mtns_leaf and mtns_map to use it.

    Create mtns_leaf and mtns_map as you did before.

    Then change addMarkers to addCircleMarkers and keep all of the arguments the same.

Showing off our colors

  • To add the colors to our plot, use the addCircleMarkers like before but this time include color = ~state_colors as an argument.

  • It's hard to know just what the different colors mean so let's add a legend.

    • First, assign the map with the circle markers as mtns_map.

    • Then, fill in the blanks below to place a legend in the top-right hand corner.

      addLegend(_, colors = ~unique(_), labels = ~unique(____)).