plot_usmap with ggplot2The 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(include = c("CA", "ID", "NV", "OR", "WA")) +
labs(title = "Western US States",
subtitle = "These are the states in the Pacific Timezone.")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 = FLYou 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] -2390688 -2387144 -2385403 -2381808 -2383378 ...
#> ..$ :List of 1
#> .. ..$ : num [1:14, 1:2] -2421123 -2418972 -2411531 -2404828 -2402353 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2421477 -2418935 -2417999 -2420152 -2421314 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -2431161 -2427289 -2426334 -2426972 -2429909 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2431855 -2429938 -2428889 -2429380 -2431964 ...
#> ..$ :List of 1
#> .. ..$ : num [1:20, 1:2] -2460029 -2456927 -2454788 -2451596 -2448993 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2546091 -2540802 -2538816 -2538986 -2544230 ...
#> ..$ :List of 1
#> .. ..$ : num [1:12, 1:2] -2563369 -2559866 -2556791 -2553131 -2556338 ...
#> ..$ :List of 1
#> .. ..$ : num [1:20, 1:2] -2583384 -2577340 -2573060 -2564056 -2561139 ...
#> ..$ :List of 1
#> .. ..$ : num [1:77, 1:2] -995482 -993129 -992198 -991411 -991958 ...
#> ..$ :List of 1
#> .. ..$ : num [1:63, 1:2] -1028654 -1026842 -1024708 -1023942 -1020501 ...
#> ..$ :List of 1
#> .. ..$ : num [1:45, 1:2] -1076257 -1072785 -1069351 -1066830 -1066041 ...
#> ..$ :List of 1
#> .. ..$ : num [1:78, 1:2] -1111408 -1111895 -1109805 -1108349 -1110959 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -1554894 -1552649 -1549532 -1550566 -1551737 ...
#> ..$ :List of 1
#> .. ..$ : num [1:8, 1:2] -1588623 -1585891 -1583836 -1581859 -1582607 ...
#> ..$ :List of 1
#> .. ..$ : num [1:121, 1:2] -1608701 -1606747 -1605064 -1602725 -1599728 ...
#> ..$ :List of 1
#> .. ..$ : num [1:14, 1:2] -1604364 -1601124 -1596099 -1587321 -1584055 ...
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -1627275 -1625709 -1623661 -1621346 -1621629 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -1657811 -1656434 -1654243 -1654087 -1655836 ...
#> ..$ :List of 1
#> .. ..$ : num [1:8, 1:2] -1676496 -1673257 -1670826 -1667267 -1669250 ...
#> ..$ :List of 1
#> .. ..$ : num [1:24, 1:2] -1757988 -1756550 -1756659 -1755316 -1752054 ...
#> ..$ :List of 1
#> .. ..$ : num [1:19, 1:2] -1777341 -1776044 -1772492 -1767347 -1762003 ...
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -1789067 -1788516 -1784782 -1778901 -1778285 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -1794182 -1792112 -1791302 -1791596 -1794859 ...
#> ..$ :List of 1
#> .. ..$ : num [1:5, 1:2] -1802384 -1797220 -1798192 -1802119 -1802384 ...
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -1835867 -1827860 -1820421 -1821704 -1824056 ...
#> ..$ :List of 1
#> .. ..$ : num [1:14, 1:2] -1920106 -1915975 -1910208 -1905914 -1901830 ...
#> ..$ :List of 1
#> .. ..$ : num [1:13, 1:2] -1934092 -1930947 -1928153 -1920680 -1916190 ...
#> ..$ :List of 1
#> .. ..$ : num [1:44, 1:2] -1954353 -1951268 -1946003 -1941602 -1939148 ...
#> ..$ :List of 1
#> .. ..$ : num [1:58, 1:2] -1988599 -1985507 -1982287 -1978047 -1975315 ...
#> ..$ :List of 1
#> .. ..$ : num [1:1245, 1:2] -1932890 -1929114 -1921577 -1913002 -1900860 ...
#> ..$ :List of 1
#> .. ..$ : num [1:33, 1:2] -2036582 -2028705 -2025934 -2022659 -2020219 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2037520 -2031769 -2027800 -2027418 -2031313 ...
#> ..$ :List of 1
#> .. ..$ : num [1:16, 1:2] -2064322 -2061420 -2059278 -2054122 -2050942 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2051199 -2047795 -2043338 -2043410 -2047971 ...
#> ..$ :List of 1
#> .. ..$ : num [1:8, 1:2] -2085142 -2080322 -2076015 -2078002 -2081197 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -2101236 -2099338 -2097542 -2098768 -2101144 ...
#> ..$ :List of 1
#> .. ..$ : num [1:54, 1:2] -2034221 -2032618 -2031844 -2029803 -2026867 ...
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -2142932 -2140571 -2137363 -2133659 -2133046 ...
#> ..$ :List of 1
#> .. ..$ : num [1:13, 1:2] -2093048 -2090033 -2089266 -2082743 -2078886 ...
#> ..$ :List of 1
#> .. ..$ : num [1:37, 1:2] -2228763 -2220057 -2215311 -2209935 -2204698 ...
#> ..$ :List of 1
#> .. ..$ : num [1:39, 1:2] -2283095 -2281187 -2276684 -2275798 -2276826 ...
#> ..$ :List of 1
#> .. ..$ : num [1:17, 1:2] -2305126 -2299351 -2293704 -2290844 -2289077 ...
#> ..$ :List of 1
#> .. ..$ : num [1:19, 1:2] -2320076 -2316655 -2312400 -2310960 -2306956 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2344705 -2337623 -2333827 -2335286 -2340149 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -2341800 -2338668 -2337871 -2340054 -2342947 ...
#> ..$ :List of 1
#> .. ..$ : num [1:7, 1:2] -2356315 -2349468 -2347609 -2347445 -2353387 ...
#> ..- 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] -1757988 -1756550 -1756659 -1755316 -1752054 ...
#> ..$ :List of 1
#> .. ..$ : num [1:19, 1:2] -1777341 -1776044 -1772492 -1767347 -1762003 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -1794182 -1792112 -1791302 -1791596 -1794859 ...
#> ..$ :List of 1
#> .. ..$ : num [1:5, 1:2] -1802384 -1797220 -1798192 -1802119 -1802384 ...
#> ..$ :List of 1
#> .. ..$ : num [1:9, 1:2] -1835867 -1827860 -1820421 -1821704 -1824056 ...
#> ..$ :List of 1
#> .. ..$ : num [1:157, 1:2] -1899960 -1897388 -1893669 -1891633 -1888660 ...
#> ..$ :List of 1
#> .. ..$ : num [1:14, 1:2] -1920106 -1915975 -1910208 -1905914 -1901830 ...
#> ..$ :List of 1
#> .. ..$ : num [1:13, 1:2] -1934092 -1930947 -1928153 -1920680 -1916190 ...
#> ..$ :List of 1
#> .. ..$ : num [1:6, 1:2] -1939306 -1937657 -1934697 -1937360 -1939329 ...
#> ..- 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.