This vignette will explore some of the more advanced mapping featuresofusmap. Before continuing, be sure to check outMapping the US as that will cover more of thebasics of plotting US maps and styling them withggplot2.
As ofusmap 0.4.0, maps with state labels can becreated:
usmap 0.5.0 adds the ability to add county labels:
Labels can be colored using thelabel_colorparameter:
ggplot2 aesthetic mapping parametersParameters used by the map’s aesthetic mapping(ggplot2::aes) can be passed directly viaplot_usmap by adding the parameters anywhere at the callsite:
usmap::plot_usmap("counties",include =c("MA","CT","RI"),labels =TRUE,label_color ="blue",fill ="yellow",alpha =0.25,color ="orange",linewidth =2)Notice in this case we set thefill andalpha parameters to fill in the counties with asemi-transparent yellow color.
The following parameters are supported:
fill: fill color of the state/county polygonsalpha: transparency of the state/county polygon fillcolorscolor/colour: line color of thestate/county polygonslinewidth: thickness of the state/county polygonlinesusmap projectionData sets with longitude and latitude coordinates can be transformedto match the projection used inusmap (Albers Equal Areaprojection). This is convenient for plotting location-specific data andvalues usingggplot2 layers such asgeom_pointandgeom_label.
The projection used byusmap can also be accessed byusingusmap_crs():
usmap::usmap_crs()#> Coordinate Reference System:#> User input: EPSG:9311#> wkt:#> PROJCRS["NAD27 / US National Atlas Equal Area",#> BASEGEOGCRS["NAD27",#> DATUM["North American Datum 1927",#> ELLIPSOID["Clarke 1866",6378206.4,294.978698213898,#> LENGTHUNIT["metre",1]]],#> PRIMEM["Greenwich",0,#> ANGLEUNIT["degree",0.0174532925199433]],#> ID["EPSG",4267]],#> CONVERSION["US National Atlas Equal Area",#> METHOD["Lambert Azimuthal Equal Area (Spherical)",#> ID["EPSG",1027]],#> PARAMETER["Latitude of natural origin",45,#> ANGLEUNIT["degree",0.0174532925199433],#> ID["EPSG",8801]],#> PARAMETER["Longitude of natural origin",-100,#> ANGLEUNIT["degree",0.0174532925199433],#> ID["EPSG",8802]],#> PARAMETER["False easting",0,#> LENGTHUNIT["metre",1],#> ID["EPSG",8806]],#> PARAMETER["False northing",0,#> LENGTHUNIT["metre",1],#> ID["EPSG",8807]]],#> CS[Cartesian,2],#> AXIS["easting (X)",east,#> ORDER[1],#> LENGTHUNIT["metre",1]],#> AXIS["northing (Y)",north,#> ORDER[2],#> LENGTHUNIT["metre",1]],#> USAGE[#> SCOPE["Statistical analysis."],#> AREA["United States (USA) - onshore and offshore."],#> BBOX[15.56,167.65,74.71,-65.69]],#> ID["EPSG",9311]]A convenience method calledusmap_transform is providedthat transforms adata.frame containing longitude/latitudecolumns to use this projection. (Currently, onlydata.frames are supported. Other structures may besupported in the future.)
Here is an example using the providedearthquakesdataset:
library(usmap)library(ggplot2)eq_transformed<-usmap_transform(earthquakes)plot_usmap()+geom_sf(data = eq_transformed,aes(size = mag),color ="red",alpha =0.25)+labs(title ="US Earthquakes",subtitle ="Source: USGS, Jan 1 to Jun 30 2019",size ="Magnitude")+theme(legend.position ="right")And a more comprehensive example using the providedcitypop data set:
library(usmap)library(ggplot2)cities_t<-usmap_transform(citypop)plot_usmap(fill ="yellow",alpha =0.25)+geom_sf(data = cities_t,aes(size = city_pop),color ="purple",alpha =0.5)+ ggrepel::geom_label_repel(data = cities_t,aes(label = most_populous_city,geometry = geometry),size =3,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)+scale_size_continuous(range =c(1,16),label = scales::comma)+labs(title ="Most Populous City in Each US State",subtitle ="Source: US Census 2010",size ="City Population")+theme(legend.position ="right")Here is an example of transforming and plotting anothersf object on the map, using the providedrivers dataset. In this example the width of the rivercorresponds to its length, and the color indicates the river system itbelongs to.
library(usmap)library(ggplot2)rivers_t<-usmap_transform(usrivers)plot_usmap("counties",color ="gray80")+geom_sf(data = rivers_t,aes(linewidth = Shape_Length,color = SYSTEM,fill = SYSTEM))+scale_linewidth_continuous(range =c(0.3,1.8),guide ="none")+scale_color_discrete(guide ="none")+labs(title ="Major Rivers in the United States",subtitle ="Source: ESRI 2010",fill ="River System")+theme(legend.position ="right")Theusmap_transform function, combined with the power ofggplot2 layers can allow for some very unique and complexdata visualizations on the US map. Theusmap_transformfunction also handles transforming points in the Alaska/Hawaii area sothat they are appropriately displayed on their respective states.