-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-gis.Rmd
108 lines (83 loc) · 3.45 KB
/
02-gis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---
title: "Working with Maps and GIS data"
#author: "Liming Wang"
#date: "8/21/2017"
output:
html_document:
toc: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Notable packages
### The tidycensus packages
- Website: https://walkerke.github.io/tidycensus/
- Install with `install.packages("tidycensus")`
- Vignettes: https://walkerke.github.io/tidycensus/articles/basic-usage.html
- Reference: https://walkerke.github.io/tidycensus/reference/index.html
### The simple feature packages
- Website: https://github.com/r-spatial/sf
- Install with `install.packages("sf")`
- On Mac, you may need to install `udunits` first, for example, with [homebrew](https://stackoverflow.com/a/42387825/688693)
- On Linux, install `libudunits2-dev` with your software management system first
- Vignettes: https://r-spatial.github.io/sf/articles/sf1.html
- Reference: https://r-spatial.github.io/sf/reference/index.html
## Code examples
### Data files used in the examples
1. [Portland Metro 1994 TAZ shape file](data/taz1260.shp.zip): Download & unzip to `data` subdirectory of your RStudio project folder
2. [Portland Metro 1994 Survey geocode.raw.zip](data/portland94_geocode.raw.zip)
### Map ACS 2012-16 Median Household Income by Census Tract
```{r}
## Install the tidycensus package if you haven't yet
#install.packages("tidycensus")
library(tidycensus)
library(ggplot2)
library(dplyr)
## setup cenus api key
## signup your census api key at http://api.census.gov/data/key_signup.html
#census_api_key("YOUR_CENSUS_API_KEY") #
portland_tract_medhhinc <- get_acs(geography = "tract",
year = 2016, # 2012-2016
variables = "B19013_001", # Median Household Income in the Past 12 Months
state = "OR",
county = c("Multnomah County", "Washington County", "Clackamas County"),
geometry = TRUE) # load geometry/gis info
(myplot <- ggplot(portland_tract_medhhinc) +
geom_sf(aes(fill = estimate)) +
coord_sf(datum = NA) + theme_minimal())
ggsave("output/mymap.pdf", myplot)
```
### Interactive Maps of ACS 2012-16 Median Household Income by Census Tract
```{r, results="asis"}
## Install the mapview package if you haven't yet
#install.packages("mapview")
library(sf)
library(mapview)
library(dplyr)
mapview(portland_tract_medhhinc %>% select(estimate),
col.regions = sf.colors(10), alpha = 0.1)
```
### Example of spatial analysis: spatial join
```{r}
library(sf)
library(readr)
# read 1994 Metro TAZ shape file
taz_sf <- st_read("data/taz1260.shp", crs=2913)
# read geocode.raw file that contains X and Y coordinates
portland94_df <- read_csv("data/portland94_geocode.raw.zip", col_names=c("uid", "X", "Y", "case_id",
"freq", "rtz", "sid",
"totemp94", "retemp94"))
portland94_df <- portland94_df %>%
filter(X!=0, Y!=0) %>%
sample_n(500)
# create a point geometry with x and y coordinates in the data frame
portland94_sf <- st_as_sf(portland94_df, coords = c("X", "Y"), crs = 2913)
# spatial join to get TAZ for observations in portland94_sf
portland94_sf <- st_join(portland94_sf, taz_sf)
head(portland94_sf)
ggplot() +
geom_sf(data = taz_sf, aes(alpha=0.9)) +
geom_sf(data = portland94_sf)
```
# Resources:
- [R spatial CRAN Task View](https://cran.r-project.org/web/views/Spatial.html)