forked from libjohn/mapping-with-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_choropleth.Rmd
More file actions
159 lines (105 loc) · 5.9 KB
/
02_choropleth.Rmd
File metadata and controls
159 lines (105 loc) · 5.9 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
title: "tidycensus"
output: html_document
---
The [tidycensus](https://walkerke.github.io/tidycensus/index.html) package, developed by [Kyle Walker](https://walkerke.github.io/), is very **convenient and easy to use package for making choropleth maps** from United States Department of **Census data**, specifically from the Decennial and ACS Census reports. This package makes it possible to gather census variables and conveniently join those variables with "Census Geography" (i.e. aka "shapefiles", or polygons.) Visualization, or plotting, maps can be done with separate packages.
```{r libraries, message=FALSE, warning=FALSE}
library(tidyverse)
library(sf)
library(tidycensus)
library(leaflet)
library(mapview)
```
## Census API Key
Use the tidycensus package to gather Census data and join that data with Census geography (i.e. geometry, i.e. shapefiles, i.e. polygons). First, you will need to get a [free Census API key](https://api.census.gov/data/key_signup.html). Kyle Walker's [*Basic usage of tidycensus*](https://walkerke.github.io/tidycensus/articles/basic-usage.html) documents this process.
```{}
census_api_key("YOUR API KEY GOES HERE")
```
### .Renviron File
See also Kyle's [more detailed documentation](https://walkerke.github.io/tidycensus/reference/census_api_key.html) for putting the key into your environment file. But skip that for the moment.
## TidyCensus -- Get Data
Create a Simple Features dataframe using `tidycensus::get_acs()`
The Census population variable we'll use is "B01003_001". More information about identifying Census variables is available at the [bottom of this page](#variables).
```{r load-data, message=FALSE, warning=FALSE, include=FALSE}
nc_pop <-
get_acs(geography = "county",
variables = "B01003_001",
state = "NC",
geometry = TRUE)
#nc_pop
```
```{}
nc_pop <-
get_acs(geography = "county",
variables = "B01003_001",
state = "NC",
geometry = TRUE)
```
## Make Choropleth via mapview
Identify which variable will be used to create the color ramp shading. Assign this variable with the `zcol` argument. The `estimate` variable was extracted via the `tidycensus::get_acs()` function.
```{r make_choropleth}
mapview(nc_pop, zcol = "estimate")
```
## Add another layer
Now we'll geolocate the Starbucks stores and add those locations as a layer over our choropleth. The Starbucks locations were generated and plotted in the previous exercise. Here we regenerate the StarbuckNC object.
### Load Lat/Long Data
```{r load-data_02, message=FALSE, warning=FALSE}
starbucks <- read_csv("data/All_Starbucks_Locations_in_the_US_-_Map.csv")
```
Subset Starbucks Data to North Carolina
```{r filter-dataset}
starbucksNC <- starbucks %>%
filter(State == "NC")
```
Convert the `starbuckNC` dataframe to a spatial (sf) object and assign the same projection as the `nc_pop` spatial object.
```{r convert2sf}
starbucksNC <- st_as_sf(starbucksNC, coords = c("Longitude", "Latitude"), crs = st_crs(nc_pop))
```
Generate the map with multiple layers. You can read more about additional arguments such as `homebutton, legend, alpha, cex` in the [`mapview()` documentation](https://r-spatial.github.io/mapview/reference/mapView.html). Read about the many more mapview functions in the [full documentation](https://r-spatial.github.io/mapview/reference/).
```{r}
mymap <- mapview(nc_pop,
zcol = "estimate",
homebutton = FALSE) +
mapview(starbucksNC,
zcol = "Name",
legend = FALSE,
alpha = 0.5, cex = 3,
col.regions = "orange",
homebutton = FALSE)
addLogo(mymap, "images/Rfun3.png",
position = "bottomright",
offset.x = 8,
offset.y = 38,
width = 100,
height = 100)
```
## Alaska & Hawaii - Shift
Shift and re-scale Alaska and Hawaii for better cartographic display of the entire US.
```{r shiftgeoakhi, message=FALSE, warning=FALSE}
population <- get_acs(geography = "state",
variables = "B01003_001",
geometry = TRUE,
shift_geo = TRUE)
```
```{r buildshift, message=FALSE, warning=FALSE}
mapviewOptions(legend.pos = "bottomright")
mapviewOptions(leafletWidth = 800)
#mapviewOptions()
#mapviewOptions(default = TRUE)
mapview(population, zcol = "estimate", native.crs = TRUE, crs = 5070)
```
## Census
During the workshop I will discuss the following concepts in more detail.
- ACS v Decennial
- Variable Names / Numbers
- More on Census Geography (shapefiles)
### Variables
The Census is a very large collection of data. Many casual users of Census data are interested in a single data point, for example population by County. Given the complexity and richness of available Census data, finding a useful data variable can be quite a bit of work. The links below are a some methods one might use to identify the proper code name with a Census data variable.
- [Searching for Variables](https://walkerke.github.io/tidycensus/articles/basic-usage.html#searching-for-variables)
- See Also [Tiger Metadata](https://www2.census.gov/geo/tiger/TIGER_DP/2015ACS/Metadata/STATE_METADATA_2015.txt)
- See Also [SocialExplorer Tables](https://www.socialexplorer.com/data/ACS2015_5yr/metadata/?ds=ACS15_5yr)
## Shapefiles
In the [*tmap* section](031_thematic_mapping.html) of the [Simple Features module](030_thematic_sf.html) we will use the `tigris` package to gather State level, Census geography shapefiles. Shapefiles are an important GIS data standard. There are many other standards, although shapefiles have a very broad user base. If you need shapefiles for other geographies, please consult our [*GIS Data: Starting Points* guide](https://guides.library.duke.edu/gisdata) -- a very handy collection of GIS data sources, much of which will be in the shapefiles format.
## End Notes
This session based on
- Kyle Walker's [TidyCensus](https://walkerke.github.io/tidycensus/) package