Geography & Maps
Getting Started
- Open a new, blank R Script in RStudio to work on!
- Start it by listing the R libraries we’ll need for today’s work:
library(tidyverse)
library(sf)
library(leaflet)
library(htmlwidgets)
library(webshot)
1.0 Shapefiles
Reading in a shapefile
county_shp <- st_read("path/to/file/geography.shp", stringsAsFactors=FALSE)
2.0 Maps in ggplot
2.1 Shading by a variable, so let’s do some data set-up
We’ve got a map of Michigan counties, but let’s visualize our Lyme disease cases by county. In order to do this, we need a data frame with a column for County, and a column for the count of Lyme disease cases.
2.2 Shading by a variable, so back to shapefiles
We need a data file that looks like:
GeographyName | GeographyShapeInfo | Metric |
---|---|---|
Washtenaw | (123…) | 4 |
… | … | … |
2.3 Making the Map & Saving it as an Image File
Try it Yourself!
Using the lyme_disease
file and the county_shp
file, make a ggplot choropleth map of a different metric!
Steps: - Make the county level data frame of the metric of your choice - Merge it onto the county shape file - Make a map!
Need an idea of what to make? - Map only confirmed cases - Make a map of all cases, faceted by Investigator - Map only cases with a Referral_Date of 2020-01-12 - Map only cases occurring within a specific age group
2.4 Adding in Point information
Sometimes, you’ll have latitude and longitude information, identifying a geographic point, and in order to map it, you’ll need to turn it into a “geographic object”.
2.5 Making the Map
3.0 Maps in leaflet
leaflet is a more “elaborate” mapping capability. It allows for features like tool-tips, easy integration of underlying “classic” map images, and interactivity.
3.1 Data Set-Up - Shape Files and Feature Prep
3.2 Making the Map
3.3 Data Set-Up - Shape file data, Metric data, Color Palettes
3.4 Making the Map
3.5 Saving an Image of the Map
Try it Yourself!
Using the lyme_disease
file and the county_shp
file, make a leaflet choropleth or point map of a different metric. Try using a different base map, or adding a tooltip!