-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
README.Rmd
279 lines (192 loc) Β· 9.29 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
dpi = 300,
comment = "#>",
fig.align = "center",
fig.path = "man/figures/README-",
fig.width = 15,
dev.args = list(bg = "transparent")
)
```
# πΊ usmap
<!-- badges: start -->
[![CRAN](http://www.r-pkg.org/badges/version/usmap?color=blue)](https://cran.r-project.org/package=usmap)
[![Downloads](http://cranlogs.r-pkg.org/badges/grand-total/usmap)](https://cran.r-project.org/package=usmap)
[![check](https://github.com/pdil/usmap/actions/workflows/check.yaml/badge.svg)](https://github.com/pdil/usmap/actions/workflows/check.yaml)
[![codecov](https://codecov.io/gh/pdil/usmap/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pdil/usmap)
<!-- badges: end -->
```{r header, echo=FALSE, fig.height=8}
library(usmap)
library(ggplot2)
# City population county map (West Region) ####
citypop_t <- usmap_transform(citypop)
citypop_t_west <- citypop_t[(citypop_t$abbr %in% .west_region) & (citypop_t$abbr != "AK") & (citypop_t$abbr != "HI"), ]
west_county_citypop_map <-
plot_usmap("counties", include = .west_region, exclude = c("AK", "HI"), color = "blue", fill = "lightblue") +
geom_sf(data = citypop_t_west, aes(size = city_pop), color = "red", alpha = 0.7) +
scale_size_continuous(range = c(2, 15), guide = "none") +
ggrepel::geom_label_repel(
data = citypop_t_west,
aes(label = most_populous_city, geometry = geometry),
size = 5, alpha = 0.8,
label.r = unit(0.5, "lines"), label.size = 0.5,
segment.color = "red", segment.size = 1,
stat = "sf_coordinates", seed = 1002,
max.overlaps = 20
)
# Population by state with labels ####
state_pop_map_labeled <-
plot_usmap(data = statepop, values = "pop_2022", labels = TRUE) +
scale_fill_continuous(low = "white", high = "red", guide = "none")
# Blank county map (Alaska) ####
ak_county_map <-
plot_usmap("counties", include = "AK", color = "red", fill = "#fffdcf")
# Poverty by county (South) ####
south_pov_map <-
plot_usmap("counties", data = countypov, values = "pct_pov_2021",
include = .south_region, color = "white", size = 0) +
scale_fill_continuous(low = "darkgreen", high = "yellow", guide = "none")
# Poverty percentage by county ####
county_pov_map <-
plot_usmap(data = countypov, values = "pct_pov_2021", size = 0.2) +
scale_fill_continuous(low = "blue", high = "yellow", guide = "none")
# Rivers map ####
rivers_t <- usmap_transform(usrivers)
rivers_map <- plot_usmap("counties", color = "gray80") +
geom_sf(data = rivers_t, aes(linewidth = Shape_Length), color = "blue") +
scale_linewidth_continuous(range = c(0.3, 1.5), guide = "none")
# Combine plots ####
cowplot::plot_grid(
west_county_citypop_map,
state_pop_map_labeled,
ak_county_map,
south_pov_map,
county_pov_map,
rivers_map,
nrow = 2
)
```
## Purpose
Typically in R it is difficult to create nice US [choropleths](https://en.wikipedia.org/wiki/Choropleth_map) that include Alaska and Hawaii. The functions presented here attempt to elegantly solve this problem by manually moving these states to a new location and providing a simple features ([`sf`](https://github.com/r-spatial/sf)) object for mapping and visualization. This allows the user to easily add spatial data or features to the US map.
## Shape Files
The shape files that we use to plot the maps in R are located in the [`usmapdata`](https://github.com/pdil/usmapdata) package. These are generated from the [US Census Bureau cartographic boundary files](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.html). Maps at both the state and county levels are included for convenience.
#### Update History
| Date | `usmap` version | Shape File Year | Link |
|------------------|:----------------:|:----------------:|:----------------:|
| January 20, 2024 | 0.7.0 | 2022 | [π](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.2022.html) |
| February 27, 2022 | 0.6.0 | 2020 | [π](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.2020.html) |
| June 3, 2018 | 0.3.0 | 2017 | [π](https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.2017.html) |
| January 29, 2017 | 0.1.0 | 2015 | [π](https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.2015.html) |
## Installation
π¦ To install from CRAN (recommended), run the following code in an R console:
```{r install, eval=FALSE}
install.packages("usmap")
```
### Developer Build
β οΈ The developer build may be unstable and not function correctly, use with caution.
To install the package from this repository, run the following code in an R console:
```{r install_dev, eval=FALSE}
install.package("devtools")
devtools::install_github("pdil/usmap")
```
This method will provide the most recent developer build of `usmap`.
To begin using `usmap`, import the package using the `library` command:
```{r load}
library(usmap)
```
## Documentation
To read the package vignettes, which explain helpful uses of the package, use `vignette`:
```{r vignettes, eval=FALSE}
vignette(package = "usmap")
vignette("usmap1", package = "usmap") # 1. Introduction
vignette("usmap2", package = "usmap") # 2. Mapping the US
vignette("usmap3", package = "usmap") # 3. Advanced Mapping
```
For further help with this package, open an [issue](https://github.com/pdil/usmap/issues) or ask a question on Stack Overflow with the [usmap tag](https://stackoverflow.com/questions/tagged/usmap).
## Features
### Map Plots
- Plot US maps
```{r plots, fig.align='center'}
states <- plot_usmap("states")
counties <- plot_usmap("counties")
cowplot::plot_grid(states, counties, nrow = 1)
```
- Display only certain states, counties, or regions
```{r more_plots}
library(ggplot2)
mt <- plot_usmap("states", include = .mountain, labels = TRUE)
fl <- plot_usmap("counties", data = countypov, values = "pct_pov_2021", include = "FL") +
scale_fill_continuous(low = "green", high = "red", guide = "none")
ne <- plot_usmap("counties", data = countypop, values = "pop_2022", include = .new_england) +
scale_fill_continuous(low = "blue", high = "yellow", guide = "none")
cowplot::plot_grid(mt, fl, ne, nrow = 1)
```
- Transform and add spatial data to map
```{r sf_plot}
library(ggplot2)
# Transform included `usrivers` data set
rivers_transformed <- usmap_transform(usrivers)
river_map <- plot_usmap("counties", color = "gray80") +
geom_sf(data = rivers_transformed, aes(linewidth = Shape_Length), color = "blue") +
scale_linewidth_continuous(range = c(0.3, 1.5), guide = "none")
# Transform included `earthquakes` data set
eq_transformed <- usmap_transform(earthquakes)
earthquake_map <- plot_usmap() +
geom_sf(data = eq_transformed, aes(size = mag), color = "red", alpha = 0.25) +
scale_size_continuous(guide = "none")
cowplot::plot_grid(river_map, earthquake_map, nrow = 1)
```
### Map Data
- Obtain map data with certain region breakdown
```{r state_map}
us_map(regions = "states")
```
```{r county_map}
us_map(regions = "counties")
```
### FIPS Codes
- Look up FIPS codes for states and counties
```{r fips}
fips("New Jersey")
fips(c("AZ", "CA", "New Hampshire"))
fips("NJ", county = "Mercer")
fips("NJ", county = c("Bergen", "Hudson", "Mercer"))
```
- Retrieve states or counties with FIPS codes
```{r fips_info}
fips_info(c("34", "35"))
fips_info(c("34021", "35021"))
```
- Add FIPS codes to data frame
```{r fips_merge, message=FALSE}
library(dplyr)
data <- data.frame(
state = c("NJ", "NJ", "NJ", "PA"),
county = c("Bergen", "Hudson", "Mercer", "Allegheny")
)
data %>% rowwise %>% mutate(fips = fips(state, county))
```
## Additional Information
### Citation Information
The images generated by `usmap` are not under any copyright restrictions and may be used and distributed freely in any publication or otherwise.
The underlying shapefiles used to generate the map data are derived from the [US Census Bureau's TIGER/Line Shapefiles](https://www.census.gov/geographies/mapping-files/time-series/geo/tiger-line-file.2022.html#list-tab-790442341) which are not copyrighted but do suggest citation. See [section 1.2 of this document](https://www2.census.gov/geo/pdfs/maps-data/data/tiger/tgrshp2022/TGRSHP2022_TechDoc_Ch1.pdf).
If you wish to cite `usmap` in a publication (appreciated but never required!), you may do so in the following way:
```{r citation}
citation("usmap")
```
### Coordinate System
`usmap` uses the [US National Atlas Equal Area](https://epsg.io/9311) coordinate system:
<details>
<summary><code>sf::st_crs(9311)</code></summary>
```{r crs, echo=FALSE}
sf::st_crs(9311)
```
</details>
This [coordinate reference system (CRS)](https://www.nceas.ucsb.edu/sites/default/files/2020-04/OverviewCoordinateReferenceSystems.pdf) can also be obtained with `usmap::usmap_crs()`.
## Acknowledgments
The code used to generate the map files was based on this blog post by [Bob Rudis](https://github.com/hrbrmstr): [Moving The Earth (well, Alaska & Hawaii) With R](https://rud.is/b/2014/11/16/moving-the-earth-well-alaska-hawaii-with-r/)