Mapping with R

Outline

  1. Spatial data basics
  2. Projections
  3. Spatial data in R
  4. Leaflet Window
  5. Geographic Calculations
  6. Static Maps
  7. Leaflet
  8. Shiny

Data Organization

Before we get started, let’s create our file structure so that we can all work together:

Download this dataset.

Save it in the location on your computer where you want to work for the entirety of this workshop.

Create a New Project

  • in an Existing Directory
    • the map_workshop folder

1. Spatial Data Basics



Spatial data (also called geospatial data)

  • any data that references a specific geographic area or location.

Spatial Data Types

Vector Data

Points, Lines, and Polygons


The building block of vector data is the coordinate point. Just like in a scatter plot, each coordinate is a combination of an x and y value. With spatial data, x and y represent locations on the earth.


## create dataframe to show points
example = data.frame(long=c(41.3, 41.8, 42.9), 
                     lat=c(-116.8, -116, -115.3)) 
poly_example = data.frame(x=c(41.3, 41.7, 42.9, 42.9, 41.3, 41.3), 
                          y=c(-116.8, -116.8, -116, -115.3, -115.3, -116.8))

plot(example, col="blue", type = "p", pch = 16)
plot(example, type="l", col="green", lwd=5)
plot(poly_example, type="l", col="red", lwd=5)

Points

Lines

Polygon

Longtidue and Latitude

x = longitude

  • Longitude values range from -180 to 180.
  • The Prime Meridian = 0 cuts through Africa and Europe (specifically the Royal Observatory, in Greenwich, London).

y = latitude

  • Latitude values range from -90 at the South Pole to 90 at the North Pole. All along the equator the latitude value is 0.

The confusing world of coordinates

The combination of latitude and longitude is usually called a coordinate, and can be represented as

  • ‘latitude, longitude’
  • ‘longitude, latitude’

In R, we use the ‘longitude, latitude’ order

  • Google maps uses the opposite so you always have to switch the order if you get a location from Google Maps.

Raster Data


The building block of raster data is the grid.

  • Used to represent spatially continuous phenomena
    • like elevation or temperature

Learn more about raster.

Plotting points from latitude and longitude

You can create a map of sorts with any point data that has latitude and longitude coordinates (or any x,y coordinates that refere to a location on earth).

In-class exercise: plot latitude and longitude

Step 1.

Create a new script, called georgia_title_1_schools.R

Load the tidyverse at the top of the script

Read in a csv of schools in Georgia with latitude and longitude columns

ga_schools <- read_csv("data/raw/ga_school_enrollment_2021.csv") |>
  select(school, long, lat, total_enroll)
school long lat total_enroll
7 PILLARS CAREER ACADEMY -84.37143 33.63869 96
A.L. BURRUSS ELEMENTARY SCHOOL -84.57810 33.94340 406
AARON COHN MIDDLE SCHOOL -84.82468 32.55014 498
AARON COHN REGIONAL YOUTH DETENTION CENTER -84.85224 32.52715 17
ABBOTTS HILL ELEMENTARY SCHOOL -84.19640 34.05620 574



Step 2.

Plot the schools as a regular ggplot scatterplot – with Longitude on the x-axis and Latitude on the y-axis

You might even begin to recognize the shape of Georgia:

ggplot(data=ga_schools) +
  geom_point(aes(x=long, y=lat, size = total_enroll),
             color = "blue", alpha = .5)


The shape is distorted because latitude and longitude represent points on a globe

  • the scatterplot is plotting them onto a flat surface.


Time to talk about projections…