-
Notifications
You must be signed in to change notification settings - Fork 25
/
README.Rmd
205 lines (134 loc) · 6.06 KB
/
README.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
# urbnmapr <a href="https://urbaninstitute.github.io/urbnmapr/"><img src="man/figures/hexsticker.png" align="right" height="160"/></a>
[![Travis-CI Build Status](https://travis-ci.org/UrbanInstitute/urbnmapr.svg?branch=master)](https://travis-ci.org/UrbanInstitute/urbnmapr)
The `urbnmapr` package provides state and county shapefiles that are compatible to map with `ggplot2`.
Shapefiles include Alaska and Hawaii, transformed to be displayed as insets within the continental United States. There are options to include the territories for both the state and county maps.
This package is heavily inspired by and derived in part from the [fiftystater package](https://cran.r-project.org/package=fiftystater) by William Murphy. In contrast, `urbnmapr`:
- Uses shapefiles from the US Census Bureau
- Converts the shapefile data to `sf` format
- Adds various identifiers for merging
- Includes a county-level shapefile and options to add territories
## Stay up-to-date
Sign up [here](https://app.smartsheet.com/b/form/9029a0a8254e4044a52cdebaebe343bf) to stay up-to-date with R package releases and R resources from the Urban Institute. We will not share your information and we will not email more than once per month.
## Installation
You can install the latest version of `urbnmapr` from GitHub:
```{r installation, message=FALSE, warning=FALSE, eval=FALSE}
# install.packages("devtools")
devtools::install_github("UrbanInstitute/urbnmapr")
```
## Usage
### Quick maps
The `get_urbn_map()` function can be used to call shapefiles. Using the `sf = TRUE` option will load `sf` objects. These can be used with `geom_sf()` to create base maps of the continental United States, with Alaska and Hawaii displayed as insets:
```{r sf-state, message=FALSE, warning=FALSE}
library(tidyverse)
library(urbnmapr)
states_sf <- get_urbn_map("states", sf = TRUE)
states_sf %>%
ggplot(aes()) +
geom_sf(fill = "grey", color = "#ffffff")
```
```{r sf-county, message=FALSE, warning=FALSE}
library(tidyverse)
library(urbnmapr)
counties_sf <- get_urbn_map("counties", sf = TRUE)
counties_sf %>%
ggplot(aes()) +
geom_sf(fill = "grey", color = "#ffffff")
```
The default projection is US National Atlas Equal Area.
### More maps
Maps with US territories can also be called with `get_urbn_map()`.
```{r terr}
territories_counties <- get_urbn_map(map = "territories_counties", sf = TRUE)
ggplot() +
geom_sf(territories_counties,
mapping = aes(),
fill = "grey", color = "#ffffff")
```
### Labels
Labels for states maps can be accessed with `get_urbn_labels()`. Only state labels are custom, so they can be displayed next to smaller states. Use the function to call the appropriate labels and then label the map with `geom_sf_text()`.
```{r quick-labels}
states_sf <- get_urbn_map(map = "states", sf = TRUE)
states_sf %>%
ggplot() +
geom_sf(aes(),
fill = "grey", color = "#ffffff", size = 0.25) +
geom_sf_text(data = get_urbn_labels(map = "states", sf = TRUE),
aes(label = state_abbv),
size = 3)
```
## Merging Data
The states and counties spatial data include various identifiers to simplify merging data. The states `states` tibble contains `state_fips`, `state_abbv`, and `state_name`. The `counties` tibble contains `county_fips`, `state_abbv`, `state_fips`, `county_name`, and `state_name`.
Continuous data can be mapping on a color scale.
```{r us-choropleth}
spatial_data <- left_join(get_urbn_map(map = "states", sf = TRUE),
statedata,
by = "state_name")
ggplot() +
geom_sf(spatial_data,
mapping = aes(fill = horate),
color = "#ffffff", size = 0.25) +
labs(fill = "Homeownership rate")
```
Categorical data can be mapped on a discrete color scale.
```{r county}
counties_sf <- get_urbn_map(map = "counties", sf = TRUE)
county_groups <- countydata %>%
mutate(cat_var = paste0("Group ",
sample(1:4, nrow(countydata), replace = TRUE)))
household_data <- left_join(counties_sf, county_groups, by = "county_fips")
household_data %>%
ggplot() +
geom_sf(mapping = aes(fill = cat_var),
color = NA, size = 0.05) +
labs(fill = "Categorical variable")
```
## Styles
`library(urbnmapr)` works well with [`library(urbnthemes)](https://github.com/UrbanInstitute/urbnthemes)
```{r load-theme, message=FALSE}
library(urbnthemes)
set_urbn_defaults(style = "map")
```
```{r theme-state}
states_sf <- get_urbn_map(map = "states", sf = TRUE)
states_sf %>%
left_join(statedata, by = "state_name") %>%
ggplot() +
geom_sf(mapping = aes(fill = horate),
color = "#ffffff", size = 0.25) +
scale_fill_gradientn(labels = scales::percent) +
labs(fill = "Homeownership rate") +
coord_sf(datum = NA)
```
Map smaller geographies with `filter()`. You may need to reproject the data based on which areas you are mapping.
```{r theme-counties}
household_data %>%
filter(state_name == "California") %>%
ggplot() +
geom_sf(mapping = aes(fill = medhhincome),
color = "#ffffff", size = 0.05) +
coord_sf(datum = NA) +
scale_fill_gradientn(labels = scales::dollar) +
labs(fill = "Median household income")
```
A discrete color scale can also be used for categorical data.
```{r state-discrete}
state_categorical <- left_join(get_urbn_map(map = "states", sf = TRUE),
statedata,
by = "state_name") %>%
mutate(cat_var = paste0("Group ",
sample(1:4, nrow(statedata), replace = TRUE)))
ggplot() +
geom_sf(state_categorical, mapping = aes(fill = cat_var),
color = "#ffffff") +
scale_fill_discrete() +
coord_sf(datum = NA) +
labs(fill = "Categorical variable")
```
## License
Code released under the GNU General Public License v3.0.
## Code of conduct
Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.