forked from UrbanInstitute/urbnmapr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
162 lines (115 loc) · 5.42 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
# urbnmapr <img src="man/figures/hexsticker.png" align="right" style="width:138.1334px;height:160px;"/>
[![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 in `tibble` format that is compatible to map with `ggplot2`.
Shapefiles include Alaska and Hawaii, transformed to be displayed as insets within the continental United States.
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 a `tibble` dataframe
- Adds various identifiers for merging
- Includes a county-level shapefile
## 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
`urbnmapr` contains two `tibble` dataframes for creating maps:
- `states`
- `counties`
The `states` and `counties` tibbles can be used with `geom_polygon()` and `coord_map()` to create base maps of the continental United States, with Alaska and Hawaii displayed as insets:
```{r blank-state, message=FALSE}
library(tidyverse)
library(urbnmapr)
states %>%
ggplot(aes(long, lat, group = group)) +
geom_polygon(fill = "grey", color = "#ffffff", size = 0.25) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45)
```
```{r blank-county}
counties %>%
ggplot(aes(long, lat, group = group)) +
geom_polygon(fill = "grey", color = "#ffffff", size = 0.05) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45)
```
### More maps
Additional maps can be accessed with `get_urbn_map()`. Use the function to create a tibble in your global environment and then map using the same syntax as above.
```{r get_urbn_map}
territories_counties <- get_urbn_map(map = "territories_counties")
territories_counties %>%
ggplot(aes(long, lat, group = group)) +
geom_polygon(fill = "grey", color = "#ffffff", size = 0.05) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45)
```
### Labels
Labels for all maps can be accessed with `get_urbn_labels()`. Use the function to call the appropriate labels and then label the map with `geom_text()`. Labels can be called inside of `geom_text()`:
```{r quick-labels}
states %>%
ggplot() +
geom_polygon(aes(long, lat, group = group),
fill = "grey", color = "#ffffff", size = 0.25) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
geom_text(data = get_urbn_labels(map = "states"), aes(x = long, lat, label = state_abbv),
size = 3)
```
Or before `geom_text()` is called:
```{r get_urbn_labels}
territories <- get_urbn_map(map = "territories")
labels <- get_urbn_labels(map = "territories")
territories %>%
ggplot() +
geom_polygon(aes(long, lat, group = group),
fill = "grey", color = "#ffffff", size = 0.05) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
geom_text(data = labels, aes(x = long, lat, label = state_abbv), size = 3)
```
## Merging Data
The `states` and `counties` tibbles 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`. Both tibbles can be piped into `ggplot2` to create a choropleth map.
```{r us-choropleth}
statedata %>%
left_join(states, by = "state_name") %>%
ggplot(mapping = aes(long, lat, group = group, fill = horate)) +
geom_polygon(color = "#ffffff", size = .25) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
labs(fill = "Homeownership rate")
```
```{r county}
household_data <- left_join(countydata, counties, by = "county_fips")
household_data %>%
ggplot(aes(long, lat, group = group, fill = medhhincome)) +
geom_polygon(color = "#ffffff", size = 0.05) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45)
```
## Styles
`library(urbnmapr)` works well with the [Urban Institute `ggplot2` theme.](https://github.com/UrbanInstitute/urban_R_theme)
```{r load-theme, message=FALSE}
library(urbnthemes)
set_urbn_defaults(style = "map")
```
```{r theme-state}
statedata %>%
left_join(states, by = "state_name") %>%
ggplot(mapping = aes(long, lat, group = group, fill = horate)) +
geom_polygon(color = "#ffffff", size = .25) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
scale_fill_gradientn(labels = scales::percent) +
labs(fill = "Homeownership rate")
```
```{r theme-counties}
household_data %>%
filter(state_name %in% c("Virginia", "Maryland", "District of Columbia")) %>%
ggplot(aes(long, lat, group = group, fill = medhhincome)) +
geom_polygon(color = "#ffffff", size = 0.05) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
scale_fill_gradientn(labels = scales::dollar) +
labs(fill = "Median household income")
```
## 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.