0% found this document useful (0 votes)
6 views40 pages

Chapter 4

Uploaded by

ybanceorakle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views40 pages

Chapter 4

Uploaded by

ybanceorakle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Reading in spatial

data
V I S U A L I Z I N G G E O S PAT I A L D ATA I N R

Charlo e Wickham
Assistant Professor at Oregon State
University
Median incomes in New York County
Census tracts are areas with roughly the
same number of people

Spatial objects:
Census tract polygons

Larger neighborhood polygons

Areas of water polygons

VISUALIZING GEOSPATIAL DATA IN R


Procedure
Read in shape les that describe
neighborhoods and waterways

Match up two di erent coordinate systems

Merge data from a data frame into a


SpatialPolygonsDataFrame

Polish a map to be publication ready

VISUALIZING GEOSPATIAL DATA IN R


Reading in a shape file
Vector data: data described by points, lines, polygons

Shape le is the most common format

VISUALIZING GEOSPATIAL DATA IN R


Reading in a shape file
# rgdal::readOGR() reads in vector formats water <- readOGR("water", "water-areas")
library(rgdal)
library(sp)
OGR data source with driver: ESRI Shapefile
dir() Source: "water", layer: "water-areas"
with 20 features
"water" It has 5 fields

dir("water")

"water-areas.dbf" "water-areas.prj"
"water-areas.shp" "water-areas.shx"

VISUALIZING GEOSPATIAL DATA IN R


Checking the result
summary(water)

Object of class SpatialPolygonsDataFrame


Coordinates:
min max
x -74.04731 -73.90866
y 40.68419 40.88207
Is projected: FALSE
...

plot(water)

VISUALIZING GEOSPATIAL DATA IN R


Reading in a raster file
library(rgdal) # rgdal::readGDAL() reads in raster formats to sp objects
library(raster) # raster::raster() reads in raster formats to raster objects

dir()

"usgrid_data_2000" "usgrid_data_2000_1"

dir("usgrid_data_2000")

"metadata" "usarea00.tif" "usba00.tif" "usfb00.tif" "usgrid-2000-variables.xls" "usp2500.tif"


"uspop300.tif" "uspov00.tif" "uspvp00.tif"

total_pop <- raster("usgrid_data_2000/uspop300.tif")

VISUALIZING GEOSPATIAL DATA IN R


Checking the result
total_pop

class : RasterLayer
dimensions : 3120, 7080, 22089600 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -125, -66, 24, 50 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0
data source : /Users/wickhamc/Documents/Projects/courses-visualizing-geospatial-
data-in-r/data/census_grids/usgrid_data_2000/uspop300.tif
names : uspop300
values : 0, 65535 (min, max)

VISUALIZING GEOSPATIAL DATA IN R


Let's practice!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N R
Coordinate
reference systems
V I S U A L I Z I N G G E O S PAT I A L D ATA I N R

Charlo e Wickham
Assistant Professor at Oregon State
University
proj4string()
proj4string(countries_spdf)

"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

↑ Good for global datasets ↑

proj4string(water)

"+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0"

↑ Common for US datasets ↑

VISUALIZING GEOSPATIAL DATA IN R


proj4string()
proj4string(nyc_tracts)

"+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0"

proj4string(neighborhoods)

"+proj=lcc +lat_1=40.66666666666666 +lat_2=41.03333333333333


+lat_0=40.16666666666666 +lon_0=-74 +x_0=300000 +y_0=0 +datum=NAD83

+units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0"

Lambert Conformal Conic projection

VISUALIZING GEOSPATIAL DATA IN R


Setting CRS
x <- SpatialPoints(data.frame(-123.2620, 44.5646))
x

class : SpatialPoints
features : 1
extent : -123.262, -123.262, 44.5646, 44.5646 (xmin, xmax, ymin, ymax)
coord. ref. : NA

proj4string(x) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"


x

class : SpatialPoints
features : 1
extent : -123.262, -123.262, 44.5646, 44.5646 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0

VISUALIZING GEOSPATIAL DATA IN R


Transforming CRS
rgdal::spTransform(x, CRSobj, ...)

spTransform(x, "+proj=lcc +lat_1=40.66666666666666


+lat_2=41.03333333333333 +lat_0=40.16666666666666
+lon_0=-74 +x_0=300000 +y_0=0 +datum=NAD83
+units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0")

VISUALIZING GEOSPATIAL DATA IN R


Transforming CRS
rgdal::spTransform(x, CRSobj, ...)

spTransform(x, proj4string(neighborhoods))

class : SpatialPoints
features : 1
extent : -11214982, -11214982, 5127323, 5127323 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=lcc +lat_1=40.66666666666666
+lat_2=41.03333333333333 +lat_0=40.16666666666666
+lon_0=-74 +x_0=300000 +y_0=0 +datum=NAD83
+units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0

VISUALIZING GEOSPATIAL DATA IN R


Let's practice!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N R
Adding data to
spatial objects
V I S U A L I Z I N G G E O S PAT I A L D ATA I N R

Charlo e Wickham
Assistant Professor at Oregon State
University
Income data from ACS
str(nyc_income)

'data.frame': 288 obs. of 6 variables:


$ name : chr "Census Tract 1, New York County, New York"
"Census Tract 2.01, New York County, New York"
"Census Tract 2.02, New York County, New York"
"Census Tract 5, New York County, New York" ...
$ state : int 36 36 36 36 36 36 36 36 36 36 ...
$ county : int 61 61 61 61 61 61 61 61 61 61 ...
$ tract : chr "000100" "000201" "000202" "000500" ...
$ estimate: num NA 23036 29418 NA 18944 ...
$ se : num NA 3083 1877 NA 1442 ...

VISUALIZING GEOSPATIAL DATA IN R


Tract polygon data
str(nyc_tracts, max.level = 2)

Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots


..@ data :'data.frame': 288 obs. of 9 variables:
..@ polygons :List of 288
.. .. [list output truncated]
..@ plotOrder : int [1:288] 175 225 97 209 249 232 208 247 244 207 ...
..@ bbox : num [1:2, 1:2] -74 40.7 -73.9 40.9
.. ..- attr(*, "dimnames")=List of 2
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot

VISUALIZING GEOSPATIAL DATA IN R


Tract polygon data
str(nyc_tracts@data)

'data.frame': 288 obs. of 9 variables:


$ STATEFP : chr "36" "36" "36" "36" ...
$ COUNTYFP: chr "061" "061" "061" "061" ...
$ TRACTCE : chr "001401" "002201" "003200" "004000" ...
$ AFFGEOID: chr "1400000US36061001401" "1400000US36061002201"
"1400000US36061003200" "1400000US36061004000" ...
$ GEOID : chr "36061001401" "36061002201" "36061003200" "36061004000" ...
$ NAME : chr "14.01" "22.01" "32" "40" ...
$ LSAD : chr "CT" "CT" "CT" "CT" ...
$ ALAND : num 93510 161667 217682 178340 124447 ...
$ AWATER : num 0 0 0 0 0 0 0 0 0 0 ...

Goal: Add the estimated median income to this data frame

VISUALIZING GEOSPATIAL DATA IN R


Correspondence between polygons and data
four_tracts sapply(four_tracts@polygons,
function(x) x@ID)

class : SpatialPolygons
features : 4 "156" "157" "158" "159"
extent : -73.99022, (xmin)
-73.97875, (xmax) four_data
40.71413, (ymin)
40.73329 (ymax)
TRACTCE
coord. ref. : +proj=longlat +datum=NAD83
159 004000
+no_defs +ellps=GRS80
158 003200
+towgs84=0,0,0
157 002201
156 001401

VISUALIZING GEOSPATIAL DATA IN R


Correspondence between polygons and data
SpatialPolygonsDataFrame(Sr, data, match.ID = TRUE)

SpatialPolygonsDataFrame(four_tracts, four_data)

class : SpatialPolygonsDataFrame
features : 4
extent : -73.99022, -73.97875, 40.71413, 40.73329 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0
variables : 1
names : TRACTCE
min values : 001401
max values : 004000

VISUALIZING GEOSPATIAL DATA IN R


Correspondence between polygons and data
SpatialPolygonsDataFrame(Sr, data, match.ID = TRUE)

SpatialPolygonsDataFrame(four_tracts, four_data)@data

TRACTCE
156 001401
157 002201
158 003200
159 004000

VISUALIZING GEOSPATIAL DATA IN R


Correspondence between polygons and data
SpatialPolygonsDataFrame(four_tracts, four_data, match.ID = FALSE)@data

TRACTCE
159 004000
158 003200
157 002201
156 001401

Correspondence is now lost!

VISUALIZING GEOSPATIAL DATA IN R


Adding new data
Once created, no checks that data stay matched to polygons

Recreate object being very careful to match polygons to the right rows

sp::merge() , merge() for sp objects

VISUALIZING GEOSPATIAL DATA IN R


Let's practice!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N R
Polishing a map
V I S U A L I Z I N G G E O S PAT I A L D ATA I N R

Charlo e Wickham
Assistant Professor at Oregon State
University
Polishing a map
Remove distractions, let data shine

Useful spatial context

Like any plot: check legend, title, and labels for readability

Add annotations:
Highlight important points

A ribute data sources

VISUALIZING GEOSPATIAL DATA IN R


Critiquing our map

VISUALIZING GEOSPATIAL DATA IN R


Critiquing our map

VISUALIZING GEOSPATIAL DATA IN R


Critiquing our map

VISUALIZING GEOSPATIAL DATA IN R


Critiquing our map

VISUALIZING GEOSPATIAL DATA IN R


The effect of line weights and color

VISUALIZING GEOSPATIAL DATA IN R


Let's practice!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N R
Wrap up
V I S U A L I Z I N G G E O S PAT I A L D ATA I N R

Charlo e Wickham
Assistant Professor at Oregon State
University
Final tweaks
Tweak labels "by hand"

Add $ to legend

Remove tiny areas

VISUALIZING GEOSPATIAL DATA IN R


Chapter 1
Types of spatial data: point, line, polygon and raster

Adding context using ggmap

VISUALIZING GEOSPATIAL DATA IN R


Chapters 2 & 3
Spatial classes provided by sp and raster

S4 objects

tmap for displaying spatial data

VISUALIZING GEOSPATIAL DATA IN R


Chapter 4
Reading in spatial data

Transforming coordinate systems

Adding data to Spatial___DataFrame objects

Polishing a map

VISUALIZING GEOSPATIAL DATA IN R


Thank you!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N R

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy