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 themtnsdata 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 anRScript.– 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.
load("___.Rda") -
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 themtnsdata to create theleafthat we can use for mapping.mtns_leaf <- leaflet(____) -
Then, insert
mtns_leafinto theaddTiles()function and assign the output the namemtns_map. -
Run
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
addMarkers()function. -
Fill in the blanks below with the basic map we've created and the values for latitude and longitude.
addMarkers(mtns_map = ____, lng = ~____, lat = ~____) -
Supply the
peakvariable, in a similar way as we supplied thelatandlongvariables, to thepopupargument and include it in the code above. -
Click on a marker within California and write down the name of the mountain you clicked on.
Colorize
-
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_leafandmtns_mapto use it.– Create
mtns_leafandmtns_mapas you did before.– Then change
addMarkerstoaddCircleMarkersand 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 includecolor = ~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
mtns_map.– Then, fill in the blanks below to place a legend in the top-right hand corner.
addLegend(____, colors = ~unique(____), labels = ~unique(____)).