Introduction to spatial data

Visualizing Geospatial Data in R

Charlotte Wickham

Assistant Professor at Oregon State University

What is spatial data?

  • Data is associated with locations
  • Location described by coordinates + a coordinate reference system (CRS)
  • Common CRS: longitude, latitude describes locations on the surface of the Earth
Visualizing Geospatial Data in R

House sales in Corvallis

Screen Shot 2020-06-19 at 3.28.29 PM.png

Visualizing Geospatial Data in R

House sales in Corvallis

Screen Shot 2020-06-19 at 3.28.23 PM.png

Visualizing Geospatial Data in R

House sales in Corvallis

Screen Shot 2020-06-19 at 3.28.10 PM.png

Visualizing Geospatial Data in R

House sales in Corvallis

Screen Shot 2020-06-19 at 3.28.07 PM.png

Visualizing Geospatial Data in R

House sales in Corvallis

Screen Shot 2020-06-19 at 3.27.57 PM.png

Visualizing Geospatial Data in R

House sales in Corvallis

Screen Shot 2020-06-19 at 3.27.35 PM.png

Visualizing Geospatial Data in R

House sales in a data frame

head(sales)
        lon      lat  price bedrooms baths     
1 -123.2803 44.57808 267500        5     2     
2 -123.2330 44.59718 255000        3     2     
3 -123.2635 44.56923 295000        3     2
4 -123.2599 44.59453   5000        0     1     
5 -123.2632 44.53606  13950        0     2     
6 -123.2847 44.59877 233000        3     2     
nrow(sales)
931
  • Point data: locations are points, described by a single pair of coordinates
Visualizing Geospatial Data in R

Displaying spatial data with ggplot2

library(ggplot2)


ggplot(sales, aes(lon, lat)) + geom_point()
  • Adding some location cues would be helpful

Screen Shot 2020-06-19 at 3.41.54 PM.png

Visualizing Geospatial Data in R

The ggmap package

library(ggmap)

# Coordinates for location of interest nyc <- c(lon = -74.0059, lat = 40.7128)
# 1. Download the relevant map nyc_map <- get_map(location = nyc, zoom = 10)
# 2. Display the map ggmap(nyc_map)

NYC map

Visualizing Geospatial Data in R

Let's practice!

Visualizing Geospatial Data in R

Preparing Video For Download...