plot_usmap
with ggplot2
The nice thing about usmap::plot_usmap
is it returns a
ggplot object
object, which means we can add ggplot
layers to the plot
right out of the box.
library(usmap)
library(ggplot2)
plot_usmap(regions = "counties") +
labs(title = "US Counties",
subtitle = "This is a blank map of the counties of the United States.") +
theme(panel.background = element_rect(color = "black", fill = "lightblue"))
library(usmap)
library(ggplot2)
plot_usmap(data = statepop, values = "pop_2022", color = "red") +
scale_fill_continuous(name = "Population (2022)", label = scales::comma) +
theme(legend.position = "right")
Notice the comprehensive expandability that can be applied to the map
using ggplot2
layers. For example, we might want to use a
different color scheme.
library(usmap)
library(ggplot2)
plot_usmap(data = statepop, values = "pop_2022", color = "red") +
scale_fill_continuous(
low = "white", high = "red", name = "Population (2022)", label = scales::comma
) + theme(legend.position = "right")
The data-filled map can also be filtered to show certain regions only, like the western states shown above.
library(usmap)
library(ggplot2)
plot_usmap(
data = statepop, values = "pop_2022", include = c("CA", "ID", "NV", "OR", "WA"), color = "red"
) +
scale_fill_continuous(
low = "white", high = "red", name = "Population (2022)", label = scales::comma
) +
labs(title = "Western US States", subtitle = "These are the states in the Pacific Timezone.") +
theme(legend.position = "right")
The data passed to the data
parameter in
plot_usmap()
must be a data frame with at least two
columns. One of the columns must be named "fips"
or
"state"
and contain either the FIPS code, the state
abbreviation, or the state name (for county maps only the FIPS code is
supported). The second column must be the values to be plotted for each
region. The default name of the values column is "values"
.
If a different name is used in the data frame, the name can be specified
in the values
parameter of plot_usmap
. Any
extra columns in the data frame will be ignored.
values
columnvalues
columnName of values column must be specified in values
parameter if it is not "values"
.
Abbreviations and full names can be mixed if desired.
usmap
provides some built-in regions based on the US
Census Bureau Regions and Divisions. These can be used in place of
the include
/exclude
parameters when using
us_map
or plot_usmap
and start with a
.
(dot):
This also works with county maps. The regions can also be combined
with actual state or FIPS values within the
include
/exclude
parameters:
usmap::plot_usmap("counties",
include = c(.south_region, "IA"),
exclude = c(.east_south_central, "12")) # 12 = FL
You can even include or exclude individual counties (county-level inclusions/exclusions can only be done via their FIPS codes due to duplicate county names across states; for example eight different states have an “Orange County”):
usmap::plot_usmap("counties", fill = "yellow", alpha = 0.25,
# 06065 = Riverside County, CA
include = c(.south_region, "IA", "06065"),
# 12 = FL, 48141 = El Paso County, TX
exclude = c(.east_south_central, "12", "48141"))
These parameters therefore allow for the possibility of some complex compositions of states and counties, to create the exact map that is desired.
The following divisions are supported:
.new_england
#> [1] "CT" "MA" "ME" "NH" "RI" "VT"
.mid_atlantic
#> [1] "NJ" "NY" "PA"
.east_north_central
#> [1] "IL" "IN" "MI" "OH" "WI"
.west_north_central
#> [1] "IA" "KS" "MN" "MO" "NE" "ND" "SD"
.south_atlantic
#> [1] "DC" "DE" "FL" "GA" "MD" "NC" "SC" "VA" "WV"
.east_south_central
#> [1] "AL" "KY" "MS" "TN"
.west_south_central
#> [1] "AR" "LA" "OK" "TX"
.mountain
#> [1] "AZ" "CO" "ID" "MT" "NV" "NM" "UT" "WY"
.pacific
#> [1] "AK" "CA" "HI" "OR" "WA"
Regions are composed of multiple divisions, and the following are supported:
.northeast_region # c(.new_england, .mid_atlantic)
#> [1] "CT" "MA" "ME" "NH" "RI" "VT" "NJ" "NY" "PA"
.north_central_region # c(.east_north_central, .west_north_central)
#> [1] "IL" "IN" "MI" "OH" "WI" "IA" "KS" "MN" "MO" "NE" "ND" "SD"
.midwest_region # .north_central_region (renamed in June 1984)
#> [1] "IL" "IN" "MI" "OH" "WI" "IA" "KS" "MN" "MO" "NE" "ND" "SD"
.south_region # c(.south_atlantic, .east_south_central, .west_south_central)
#> [1] "DC" "DE" "FL" "GA" "MD" "NC" "SC" "VA" "WV" "AL" "KY" "MS" "TN" "AR" "LA"
#> [16] "OK" "TX"
.west_region # c(.mountain, .pacific)
#> [1] "AZ" "CO" "ID" "MT" "NV" "NM" "UT" "WY" "AK" "CA" "HI" "OR" "WA"
The raw US map data for counties or states can be obtained for further manipulation (and joining with data).
str(usmap::us_map())
#> sf [51 × 4] (S3: sf/tbl_df/tbl/data.frame)
#> $ fips: chr [1:51] "02" "01" "05" "04" ...
#> $ abbr: chr [1:51] "AK" "AL" "AR" "AZ" ...
#> $ full: chr [1:51] "Alaska" "Alabama" "Arkansas" "Arizona" ...
#> $ geom:sfc_MULTIPOLYGON of length 51; first list element: List of 47
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -2396847 -2393297 -2391552 -2387943 -2389511 ...
#> ..$ :List of 1
#> .. ..$ : num [1:14, 1:2] -2427326 -2425172 -2417706 -2410970 -2408484 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2427715 -2425165 -2424224 -2426378 -2427542 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -2437408 -2433527 -2432564 -2433199 -2436145 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2438128 -2436206 -2435151 -2435638 -2438228 ...
#> ..$ :List of 1
#> .. ..$ : num [1:20, 1:2] -2466360 -2463251 -2461111 -2457914 -2455311 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2552747 -2547440 -2545447 -2545613 -2550871 ...
#> ..$ :List of 1
#> .. ..$ : num [1:12, 1:2] -2570037 -2566525 -2563447 -2559775 -2562982 ...
#> ..$ :List of 1
#> .. ..$ : num [1:20, 1:2] -2590162 -2584107 -2579816 -2570785 -2567855 ...
#> ..$ :List of 1
#> .. ..$ : num [1:77, 1:2] -998197 -995837 -994902 -994112 -994660 ...
#> ..$ :List of 1
#> .. ..$ : num [1:63, 1:2] -1031451 -1029633 -1027494 -1026725 -1023275 ...
#> ..$ :List of 1
#> .. ..$ : num [1:45, 1:2] -1079178 -1075696 -1072253 -1069724 -1068941 ...
#> ..$ :List of 1
#> .. ..$ : num [1:78, 1:2] -1114424 -1114913 -1112827 -1111366 -1113983 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -1559253 -1557003 -1553876 -1554913 -1556085 ...
#> ..$ :List of 1
#> .. ..$ : num [1:8, 1:2] -1593058 -1590317 -1588257 -1586273 -1587022 ...
#> ..$ :List of 1
#> .. ..$ : num [1:121, 1:2] -1613120 -1611163 -1609478 -1607135 -1604130 ...
#> ..$ :List of 1
#> .. ..$ : num [1:14, 1:2] -1608739 -1605493 -1600458 -1591655 -1588380 ...
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -1631689 -1630120 -1628070 -1625748 -1626029 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -1662321 -1660940 -1658739 -1658580 -1660335 ...
#> ..$ :List of 1
#> .. ..$ : num [1:8, 1:2] -1681081 -1677834 -1675396 -1671826 -1673814 ...
#> ..$ :List of 1
#> .. ..$ : num [1:24, 1:2] -1762715 -1761280 -1761393 -1760048 -1756780 ...
#> ..$ :List of 1
#> .. ..$ : num [1:19, 1:2] -1782141 -1780844 -1777284 -1772124 -1766769 ...
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -1794059 -1793509 -1789766 -1783871 -1783252 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -1799022 -1796947 -1796134 -1796426 -1799699 ...
#> ..$ :List of 1
#> .. ..$ : num [1:5, 1:2] -1807248 -1802072 -1803043 -1806980 -1807248 ...
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -1840780 -1832748 -1825288 -1826572 -1828929 ...
#> ..$ :List of 1
#> .. ..$ : num [1:14, 1:2] -1925218 -1921078 -1915299 -1910995 -1906900 ...
#> ..$ :List of 1
#> .. ..$ : num [1:13, 1:2] -1939250 -1936099 -1933296 -1925809 -1921308 ...
#> ..$ :List of 1
#> .. ..$ : num [1:44, 1:2] -1959916 -1956825 -1951544 -1947128 -1944667 ...
#> ..$ :List of 1
#> .. ..$ : num [1:58, 1:2] -1993842 -1990745 -1987519 -1983272 -1980533 ...
#> ..$ :List of 1
#> .. ..$ : num [1:1244, 1:2] -1938633 -1934847 -1927287 -1918687 -1906510 ...
#> ..$ :List of 1
#> .. ..$ : num [1:33, 1:2] -2041901 -2034012 -2031238 -2027960 -2025517 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2043039 -2037272 -2033292 -2032907 -2036809 ...
#> ..$ :List of 1
#> .. ..$ : num [1:16, 1:2] -2069706 -2066807 -2064662 -2059491 -2056308 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2056794 -2053382 -2048913 -2048981 -2053551 ...
#> ..$ :List of 1
#> .. ..$ : num [1:8, 1:2] -2090565 -2085738 -2081419 -2083403 -2086606 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -2106688 -2104788 -2102985 -2104209 -2106592 ...
#> ..$ :List of 1
#> .. ..$ : num [1:54, 1:2] -2040135 -2038529 -2037758 -2035713 -2032768 ...
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -2148472 -2146109 -2142896 -2139181 -2138563 ...
#> ..$ :List of 1
#> .. ..$ : num [1:13, 1:2] -2098987 -2095957 -2095183 -2088633 -2084763 ...
#> ..$ :List of 1
#> .. ..$ : num [1:37, 1:2] -2234498 -2225774 -2221019 -2215629 -2210382 ...
#> ..$ :List of 1
#> .. ..$ : num [1:39, 1:2] -2288932 -2287029 -2282518 -2281634 -2282669 ...
#> ..$ :List of 1
#> .. ..$ : num [1:17, 1:2] -2311023 -2305236 -2299576 -2296711 -2294950 ...
#> ..$ :List of 1
#> .. ..$ : num [1:19, 1:2] -2326025 -2322597 -2318331 -2316881 -2312867 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2350602 -2343508 -2339702 -2341160 -2346032 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -2347716 -2344578 -2343772 -2345956 -2348860 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2362212 -2355362 -2353495 -2353326 -2359274 ...
#> ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
#> - attr(*, "sf_column")= chr "geom"
#> - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA
#> ..- attr(*, "names")= chr [1:3] "fips" "abbr" "full"
str(usmap::us_map(regions = "counties"))
#> sf [3,144 × 5] (S3: sf/tbl_df/tbl/data.frame)
#> $ fips : chr [1:3144] "02013" "02016" "02020" "02050" ...
#> $ abbr : chr [1:3144] "AK" "AK" "AK" "AK" ...
#> $ full : chr [1:3144] "Alaska" "Alaska" "Alaska" "Alaska" ...
#> $ county: chr [1:3144] "Aleutians East Borough" "Aleutians West Census Area" "Anchorage Municipality" "Bethel Census Area" ...
#> $ geom :sfc_MULTIPOLYGON of length 3144; first list element: List of 9
#> ..$ :List of 1
#> .. ..$ : num [1:24, 1:2] -1762715 -1761280 -1761393 -1760048 -1756780 ...
#> ..$ :List of 1
#> .. ..$ : num [1:19, 1:2] -1782141 -1780844 -1777284 -1772124 -1766769 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -1799022 -1796947 -1796134 -1796426 -1799699 ...
#> ..$ :List of 1
#> .. ..$ : num [1:5, 1:2] -1807248 -1802072 -1803043 -1806980 -1807248 ...
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -1840780 -1832748 -1825288 -1826572 -1828929 ...
#> ..$ :List of 1
#> .. ..$ : num [1:157, 1:2] -1905046 -1902469 -1898742 -1896704 -1893730 ...
#> ..$ :List of 1
#> .. ..$ : num [1:14, 1:2] -1925218 -1921078 -1915299 -1910995 -1906900 ...
#> ..$ :List of 1
#> .. ..$ : num [1:13, 1:2] -1939250 -1936099 -1933296 -1925809 -1921308 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -1944465 -1942813 -1939844 -1942511 -1944488 ...
#> ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
#> - attr(*, "sf_column")= chr "geom"
#> - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA
#> ..- attr(*, "names")= chr [1:4] "fips" "abbr" "full" "county"
You can also include only certain states and counties just like in
plot_usmap
. In fact, the regions
and
include
parameters of plot_usmap
are derived
directly from their usage in us_map
.