Introduction to spatial data Working with Geospatial Data in R - - PowerPoint PPT Presentation

introduction to spatial data
SMART_READER_LITE
LIVE PREVIEW

Introduction to spatial data Working with Geospatial Data in R - - PowerPoint PPT Presentation

Working with Geospatial Data in R Introduction to spatial data Working with Geospatial Data in R What is spatial data? Data is associated with locations Location described by coordinates + a coordinate reference system (CRS)


slide-1
SLIDE 1 Working with Geospatial Data in R

Introduction to spatial data

slide-2
SLIDE 2 Working with Geospatial Data in R

What is spatial data?

  • Data is associated with locations
  • Location described by coordinates + a coordinate

reference system (CRS)

  • Common CRS: longitude, latitude describes locations
  • n the surface of the Earth
slide-3
SLIDE 3 Working with Geospatial Data in R

44.57808 N 123.2803 W

House sales in Corvallis

  • SOLD

$267,500 1112 NW 26TH ST, CORVALLIS, OR

  • 123.2803, 44.57808
longitude latitude
slide-4
SLIDE 4 Working with Geospatial Data in R

House sales in a data frame

> head(sales) lon lat price bedrooms full_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) [1] 931 location data associated with this location
  • Point data: locations are points, described by a

single pair of coordinates

slide-5
SLIDE 5 Working with 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

slide-6
SLIDE 6 Working with Geospatial Data in R

The ggmap package

> library(ggmap) > # Coordinates for the 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)
slide-7
SLIDE 7 Working with Geospatial Data in R

Let’s practice!

slide-8
SLIDE 8 Working with Geospatial Data in R

Useful get_map() and ggmap() options

slide-9
SLIDE 9 Working with Geospatial Data in R

Changing the map image

> library(ggmap) > corvallis <- c(lon = -123.2620, lat = 44.5646) > corvallis_map <- get_map(corvallis, zoom = 13, scale = 1) Map from URL : http://maps.googleapis.com/maps/api/staticmap? center=44.5646,-123.262&zoom=13&size=640x640&scale=1& maptype=terrain&language=en-EN&sensor=false
  • By default, get_map(), downloads a terrain image from

Google maps

> corvallis_map <- get_map(corvallis, zoom = 13, maptype = "terrain", source = "google")
slide-10
SLIDE 10 Working with Geospatial Data in R

Other map image sources

> ?get_map > corvallis_map <- get_map(corvallis, zoom = 13, maptype = "toner-2010", source = "stamen") … maptype = c("terrain", "terrain-background", "satellite", "roadmap", "hybrid", "toner", "watercolor", "terrain-labels", "terrain-lines", "toner-2010", "toner-2011", "toner-background", "toner-hybrid", "toner-labels", "toner-lines", "toner-lite"), source = c("google", "osm", "stamen"), …
slide-11
SLIDE 11 Working with Geospatial Data in R

Specifying default data and aesthetics

ggmap(ggmap, extent = "panel", base_layer, maprange = FALSE, legend = "right", padding = 0.02, darken = c(0, "black"), ...)
  • > ?ggmap
> ggplot(sales, aes(lon, lat)) + geom_point() > ggplot() + geom_point(aes(lon, lat), data = sales) > ggmap(corvallis_map) + geom_point(aes(lon, lat), data = sales) > ggmap(corvallis_map, base_layer = ggplot(sales, aes(lon, lat))) + geom_point() > ggmap(corvallis_map, base_layer = ggplot(sales, aes(lon, lat))) + geom_point() + facet_wrap(~ condition)
slide-12
SLIDE 12 Working with Geospatial Data in R

Changing the way the map is ploed

ggmap(ggmap, extent = "panel", base_layer, maprange = FALSE, legend = "right", padding = 0.02, darken = c(0, "black"), ...)
  • > ?ggmap
  • extent: how much of the ploing area should the map

take up?

  • maprange: should the plot limits come from the map

limits?

slide-13
SLIDE 13 Working with Geospatial Data in R

Let’s practice!

slide-14
SLIDE 14 Working with Geospatial Data in R

Common types of spatial data

slide-15
SLIDE 15 Working with Geospatial Data in R

Types of spatial data

  • Point
price: $267500 bedrooms: 5 full_baths: 2 date: 2015-12-31 …
slide-16
SLIDE 16 Working with Geospatial Data in R

Types of spatial data

  • Point
  • Line
name: Dixon Creek length: 5 miles avg_discharge: 2 m3/s …
slide-17
SLIDE 17 Working with Geospatial Data in R

Types of spatial data

  • Point
  • Line
  • Polygon
field: F01_2 area: 2 acres crop: wheat …
slide-18
SLIDE 18 Working with Geospatial Data in R

Types of spatial data

  • Point
  • Line
  • Polygon
  • Raster (a.k.a Gridded)
cover: Forest elevation: 1050m slope: 10° …
slide-19
SLIDE 19 Working with Geospatial Data in R

House prices by ward

> head(ward_sales) ward lon lat group order num_sales avg_price 1 1 -123.3128 44.56531 0.1 1 159 311626.9 2 1 -123.3122 44.56531 0.1 2 159 311626.9 3 1 -123.3121 44.56531 0.1 3 159 311626.9 4 1 -123.3119 44.56531 0.1 4 159 311626.9 5 1 -123.3119 44.56485 0.1 5 159 311626.9 6 1 -123.3119 44.56430 0.1 6 159 311626.9
  • Wards are areas that have roughly equal numbers of

people

  • Can be described by polygons
slide-20
SLIDE 20 Working with Geospatial Data in R

Drawing polygons is tricky

  • Order maers
slide-21
SLIDE 21 Working with Geospatial Data in R

Drawing polygons is tricky

  • Order maers
  • Some areas may

need more than

  • ne polygon
Ward 1 Ward 2
slide-22
SLIDE 22 Working with Geospatial Data in R

Predicted house prices

> head(preds) lon lat predicted_price 1 -123.3168 44.52539 258936.2 2 -123.3168 44.52740 257258.4 3 -123.3168 44.52940 255543.1 4 -123.3168 44.53141 253791.0 5 -123.3168 44.53342 252002.4 6 -123.3168 44.53542 250178.7
slide-23
SLIDE 23 Working with Geospatial Data in R

Let’s practice!