Sources and References

Shapefiles - States

About creating tmap insets for AK and HI

Leaflet Providers

Libraries


library(sf)
library(tidyverse)

# While running R as the administrator, reinstall ggplot2 like this:
# 
# devtools::install_github("tidyverse/ggplot2")
# require(ggplot2)

library(ggplot2)
library(tmap)

Data



states <- st_read("F:/datasets/uscensus/cb_2017_us_state_5m/cb_2017_us_state_5m.shp")
#> Reading layer `cb_2017_us_state_5m' from data source `F:\datasets\uscensus\cb_2017_us_state_5m\cb_2017_us_state_5m.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 56 features and 9 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -179.1473 ymin: -14.55255 xmax: 179.7785 ymax: 71.35256
#> epsg (SRID):    4269
#> proj4string:    +proj=longlat +datum=NAD83 +no_defs
glimpse(states)
#> Observations: 56
#> Variables: 10
#> $ STATEFP  <fct> 01, 02, 04, 08, 12, 13, 18, 20, 23, 25, 27, 34, 37, 3...
#> $ STATENS  <fct> 01779775, 01785533, 01779777, 01779779, 00294478, 017...
#> $ AFFGEOID <fct> 0400000US01, 0400000US02, 0400000US04, 0400000US08, 0...
#> $ GEOID    <fct> 01, 02, 04, 08, 12, 13, 18, 20, 23, 25, 27, 34, 37, 3...
#> $ STUSPS   <fct> AL, AK, AZ, CO, FL, GA, IN, KS, ME, MA, MN, NJ, NC, N...
#> $ NAME     <fct> Alabama, Alaska, Arizona, Colorado, Florida, Georgia,...
#> $ LSAD     <fct> 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0...
#> $ ALAND    <dbl> 1.311744e+11, 1.478588e+12, 2.941987e+11, 2.684260e+1...
#> $ AWATER   <dbl> 4592944701, 277723861311, 1027245114, 1178495763, 313...
#> $ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-88.04374 3..., MULTIPO...
levels(states$NAME)
#>  [1] "Alabama"                                     
#>  [2] "Alaska"                                      
#>  [3] "American Samoa"                              
#>  [4] "Arizona"                                     
#>  [5] "Arkansas"                                    
#>  [6] "California"                                  
#>  [7] "Colorado"                                    
#>  [8] "Commonwealth of the Northern Mariana Islands"
#>  [9] "Connecticut"                                 
#> [10] "Delaware"                                    
#> [11] "District of Columbia"                        
#> [12] "Florida"                                     
#> [13] "Georgia"                                     
#> [14] "Guam"                                        
#> [15] "Hawaii"                                      
#> [16] "Idaho"                                       
#> [17] "Illinois"                                    
#> [18] "Indiana"                                     
#> [19] "Iowa"                                        
#> [20] "Kansas"                                      
#> [21] "Kentucky"                                    
#> [22] "Louisiana"                                   
#> [23] "Maine"                                       
#> [24] "Maryland"                                    
#> [25] "Massachusetts"                               
#> [26] "Michigan"                                    
#> [27] "Minnesota"                                   
#> [28] "Mississippi"                                 
#> [29] "Missouri"                                    
#> [30] "Montana"                                     
#> [31] "Nebraska"                                    
#> [32] "Nevada"                                      
#> [33] "New Hampshire"                               
#> [34] "New Jersey"                                  
#> [35] "New Mexico"                                  
#> [36] "New York"                                    
#> [37] "North Carolina"                              
#> [38] "North Dakota"                                
#> [39] "Ohio"                                        
#> [40] "Oklahoma"                                    
#> [41] "Oregon"                                      
#> [42] "Pennsylvania"                                
#> [43] "Puerto Rico"                                 
#> [44] "Rhode Island"                                
#> [45] "South Carolina"                              
#> [46] "South Dakota"                                
#> [47] "Tennessee"                                   
#> [48] "Texas"                                       
#> [49] "United States Virgin Islands"                
#> [50] "Utah"                                        
#> [51] "Vermont"                                     
#> [52] "Virginia"                                    
#> [53] "Washington"                                  
#> [54] "West Virginia"                               
#> [55] "Wisconsin"                                   
#> [56] "Wyoming"
outside48 <- c("Alaska", "Hawaii", "Puerto Rico", "American Samoa", "United States Virgin Islands", "Guam", "Commonwealth of the Northern Mariana Islands")

contig48dc <- filter(states, !(NAME %in% outside48))
qtm(contig48dc)

tmap_mode(mode = "plot")
#> tmap mode set to plotting

tm_shape(contig48dc) +
  tm_polygons(col = "MAP_COLORS")

tmap_mode(mode = "view")
tm_shape(contig48dc) +
  tm_polygons(alpha = 0) +
  tm_view(basemaps = 'Esri.NatGeoWorldMap')