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.
- John Snow (the physician, not the character from Game of Thrones...) once famously used a map to discover how cholera was transmitted.
In this lab, we'll use
Rto create an interactive map of the
mtnsdata 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
– 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!
leaflet()function and the
mtnsdata to create the
leafthat we can use for mapping.
mtns_leaf <- leaflet(____)
addTiles()function and assign the output the name
mtns_mapin 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
Fill in the blanks below with the basic map we've created and the values for latitude and longitude.
addMarkers(map = ____, lng = ~____, lat = ~____)
peakvariable, in a similar way as we supplied the
longvariables, to the
popupargument 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
stateit's located in.
mtns <- mutate(____, state_colors = colorize(____))
Now that we've added a new variable, we need to re-build
mtns_mapto use it.
mtns_mapas you did before.
– Then change
addCircleMarkersand keep all of the arguments the same.
Showing off our colors
To add the colors to our plot, use the
addCircleMarkerslike before but this time include
color = ~state_colorsas 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
Then, fill in the blanks below to place a legend in the top-right hand corner.
addLegend(_, colors = ~unique(_), labels = ~unique(____)).