Spatial
Maryland
# data wrangling & plotting
library(tidyverse) # dplyr and ggplot2
library(reshape2)
# spatial analyses - for later
library(sf) # working with vectors - polygons, lines, etc
library(raster) # raster
library(USAboundaries)
library(USAboundariesData)
# color scales
library(viridis)
library(ggrepel)
theme_set(theme_bw())# set plotting theme for session
# get MD boundaries - USA boundaries apckage
md.state<-us_states(resolution = 'high', states='maryland')
glimpse(md.state) # look at spatial object - basically a dataframe
## Observations: 1
## Variables: 13
## $ statefp <chr> "24"
## $ statens <chr> "01714934"
## $ affgeoid <chr> "0400000US24"
## $ geoid <chr> "24"
## $ stusps <chr> "MD"
## $ name <chr> "Maryland"
## $ lsad <chr> "00"
## $ aland <dbl> 25147754905
## $ awater <dbl> 6983312282
## $ state_name <chr> "Maryland"
## $ state_abbr <chr> "MD"
## $ jurisdiction_type <chr> "state"
## $ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-76.05015 3...
# get MD county shapefile
md.cos<-us_counties(resolution = 'high', states='maryland')
unique(md.cos$name)
## [1] "Anne Arundel" "Calvert" "Caroline" "Garrett"
## [5] "Harford" "Howard" "Washington" "Baltimore"
## [9] "Cecil" "Frederick" "Montgomery" "Somerset"
## [13] "Talbot" "Allegany" "Prince George's" "Carroll"
## [17] "Charles" "Wicomico" "Kent" "Queen Anne's"
## [21] "St. Mary's" "Worcester" "Dorchester"
# make simple plot of MD counties using geom_sf
md.map<-ggplot(md.cos)+
geom_sf(fill='blue', color='white')
md.map

md<-md.state%>%st_transform(2248)
md.cos<-md.cos%>%st_transform(2248)
st_crs(md) # units are in us ft
## Coordinate Reference System:
## EPSG: 2248
## proj4string: "+proj=lcc +lat_1=39.45 +lat_2=38.3 +lat_0=37.66666666666666 +lon_0=-77 +x_0=399999.9998983998 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs"
md.pt<-md%>%st_cast('POINT') # turn MD border into points
glimpse(md.cos)
## Observations: 24
## Variables: 13
## $ statefp <chr> "24", "24", "24", "24", "24", "24", "24", "24", "...
## $ countyfp <chr> "003", "009", "011", "023", "025", "027", "043", ...
## $ countyns <chr> "01710958", "01676636", "00595737", "01711058", "...
## $ affgeoid <chr> "0500000US24003", "0500000US24009", "0500000US240...
## $ geoid <chr> "24003", "24009", "24011", "24023", "24025", "240...
## $ name <chr> "Anne Arundel", "Calvert", "Caroline", "Garrett",...
## $ lsad <chr> "06", "06", "06", "06", "06", "06", "06", "06", "...
## $ aland <dbl> 1074553083, 552178304, 827350236, 1676036320, 113...
## $ awater <dbl> 447833714, 341560888, 16777064, 22315761, 2319107...
## $ state_name <chr> "Maryland", "Maryland", "Maryland", "Maryland", "...
## $ state_abbr <chr> "MD", "MD", "MD", "MD", "MD", "MD", "MD", "MD", "...
## $ jurisdiction_type <chr> "state", "state", "state", "state", "state", "sta...
## $ geometry <MULTIPOLYGON [US_survey_foot]> MULTIPOLYGON (((1357636...
## Simple feature collection with 24 features and 12 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: 670931.1 ymin: 165995.8 xmax: 1790927 ymax: 716434.7
## epsg (SRID): 2248
## proj4string: +proj=lcc +lat_1=39.45 +lat_2=38.3 +lat_0=37.66666666666666 +lon_0=-77 +x_0=399999.9998983998 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs
## First 10 features:
## statefp countyfp countyns affgeoid geoid name lsad aland
## 178 24 003 01710958 0500000US24003 24003 Anne Arundel 06 1074553083
## 179 24 009 01676636 0500000US24009 24009 Calvert 06 552178304
## 180 24 011 00595737 0500000US24011 24011 Caroline 06 827350236
## 181 24 023 01711058 0500000US24023 24023 Garrett 06 1676036320
## 182 24 025 01698178 0500000US24025 24025 Harford 06 1132089349
## 183 24 027 01709077 0500000US24027 24027 Howard 06 649942723
## 184 24 043 01714220 0500000US24043 24043 Washington 06 1185609336
## 488 24 005 01695314 0500000US24005 24005 Baltimore 06 1549750698
## 489 24 015 00596115 0500000US24015 24015 Cecil 06 896852795
## 490 24 021 01711211 0500000US24021 24021 Frederick 06 1710554389
## awater state_name state_abbr jurisdiction_type geometry
## 178 447833714 Maryland MD state POINT (1424571 488113.5)
## 179 341560888 Maryland MD state POINT (1435743 319514.5)
## 180 16777064 Maryland MD state POINT (1644976 440922.5)
## 181 22315761 Maryland MD state POINT (670931.1 686054.5)
## 182 231910741 Maryland MD state POINT (1504928 690651.1)
## 183 6345542 Maryland MD state POINT (1331850 576906.1)
## 184 24803917 Maryland MD state POINT (1083035 706473.5)
## 488 215989606 Maryland MD state POINT (1414220 654267)
## 489 185340903 Maryland MD state POINT (1610983 695346.9)
## 490 18041931 Maryland MD state POINT (1199957 657781.8)
# use lon & lat for each centroid to place labels
md.cos$lon<-st_coordinates(st_centroid(md.cos))[,1] # add longitude to sf
md.cos$lat<-st_coordinates(st_centroid(md.cos))[,2] # add latitude to sf
# create mape of MD counties colored by land area (aland)
md.co.map<-ggplot()+
geom_sf(data=md, fill=NA, color='black')+
geom_sf(data=md.cos, aes(fill=aland))+
theme_bw()+
scale_fill_viridis(alpha=.5)+
theme(legend.position='none')
md.co.map

# label each county
md.co.map+geom_label_repel(data=md.cos, aes(x=lon, y=lat, label=name))

Texas
# data wrangling & plotting
library(tidyverse) # dplyr and ggplot2
library(reshape2)
# spatial analyses - for later
library(sf) # working with vectors - polygons, lines, etc
library(raster) # raster
library(USAboundaries)
library(USAboundariesData)
# color scales
library(viridis)
library(ggrepel)
theme_set(theme_bw())# set plotting theme for session
# get MD boundaries - USA boundaries apckage
md.state<-us_states(resolution = 'high', states='texas')
glimpse(md.state) # look at spatial object - basically a dataframe
## Observations: 1
## Variables: 13
## $ statefp <chr> "48"
## $ statens <chr> "01779801"
## $ affgeoid <chr> "0400000US48"
## $ geoid <chr> "48"
## $ stusps <chr> "TX"
## $ name <chr> "Texas"
## $ lsad <chr> "00"
## $ aland <dbl> 676633459408
## $ awater <dbl> 19025991684
## $ state_name <chr> "Texas"
## $ state_abbr <chr> "TX"
## $ jurisdiction_type <chr> "state"
## $ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-94.7183 29...
# get MD county shapefile
md.cos<-us_counties(resolution = 'high', states='texas')
unique(md.cos$name)
## [1] "Goliad" "Grimes" "Hidalgo" "Houston"
## [5] "Irion" "Kleberg" "Lamar" "Martin"
## [9] "Newton" "Orange" "Rains" "Robertson"
## [13] "San Patricio" "Smith" "Terrell" "Travis"
## [17] "Van Zandt" "Walker" "Washington" "Wharton"
## [21] "Winkler" "Wood" "Aransas" "Borden"
## [25] "Burleson" "Childress" "Ector" "Ellis"
## [29] "El Paso" "Freestone" "Galveston" "Sterling"
## [33] "Titus" "Upshur" "Val Verde" "Victoria"
## [37] "Waller" "Ward" "Webb" "Andrews"
## [41] "Angelina" "Baylor" "Bexar" "Brazoria"
## [45] "Brewster" "Burnet" "Calhoun" "Camp"
## [49] "Cass" "Coleman" "Comal" "Crockett"
## [53] "Dickens" "Glasscock" "Guadalupe" "Hardin"
## [57] "Henderson" "Hopkins" "Howard" "Jasper"
## [61] "Jefferson" "Kimble" "Leon" "Loving"
## [65] "Marion" "Mason" "Maverick" "Montgomery"
## [69] "Nacogdoches" "Navarro" "Polk" "Reagan"
## [73] "Red River" "Refugio" "Rusk" "Sabine"
## [77] "Anderson" "Fort Bend" "Motley" "Archer"
## [81] "Bosque" "Colorado" "Harris" "Hudspeth"
## [85] "Lee" "San Augustine" "Tyler" "Brazos"
## [89] "Caldwell" "Crane" "Jackson" "Pecos"
## [93] "Shelby" "Young" "Crosby" "Harrison"
## [97] "Kenedy" "Madison" "Morris" "Starr"
## [101] "Trinity" "Bee" "Eastland" "Reeves"
## [105] "Bowie" "Mills" "Presidio" "San Saba"
## [109] "Tom Green" "Delta" "McMullen" "Gaines"
## [113] "Grayson" "Hall" "Hartley" "Zavala"
## [117] "Hockley" "Jim Wells" "Jones" "La Salle"
## [121] "Live Oak" "Lubbock" "Matagorda" "Coryell"
## [125] "Culberson" "Dallas" "McCulloch" "Montague"
## [129] "Moore" "DeWitt" "Donley" "Edwards"
## [133] "Nueces" "Panola" "Runnels" "Schleicher"
## [137] "Stonewall" "Bailey" "Bastrop" "Blanco"
## [141] "Fannin" "Fayette" "Fisher" "Floyd"
## [145] "Tarrant" "Williamson" "Wise" "Zapata"
## [149] "Briscoe" "Carson" "Castro" "Cochran"
## [153] "Collin" "Jeff Davis" "Gregg" "Hale"
## [157] "Brooks" "Cameron" "Cherokee" "Coke"
## [161] "Collingsworth" "Concho" "Cottle" "Dallam"
## [165] "Deaf Smith" "Denton" "Garza" "Gray"
## [169] "Atascosa" "Bandera" "Haskell" "Hemphill"
## [173] "Medina" "Midland" "Nolan" "Ochiltree"
## [177] "Hill" "Hunt" "Jim Hogg" "Oldham"
## [181] "Palo Pinto" "Parker" "Randall" "Roberts"
## [185] "San Jacinto" "Scurry" "Shackelford" "Somervell"
## [189] "Sutton" "Johnson" "Karnes" "Austin"
## [193] "Swisher" "Taylor" "Terry" "Upton"
## [197] "Wichita" "Kaufman" "Kerr" "Kinney"
## [201] "Willacy" "Lamb" "Hardeman" "Liberty"
## [205] "Lipscomb" "Lynn" "McLennan" "Wilson"
## [209] "Rockwall" "Wheeler" "Cooke" "Callahan"
## [213] "Duval" "Erath" "Gillespie" "Kendall"
## [217] "Chambers" "Dawson" "Hamilton" "Hutchinson"
## [221] "Bell" "Gonzales" "Jack" "Lavaca"
## [225] "Parmer" "Lampasas" "Potter" "Stephens"
## [229] "Wilbarger" "Yoakum" "Comanche" "King"
## [233] "Menard" "Throckmorton" "Hood" "Mitchell"
## [237] "Sherman" "Dimmit" "Falls" "Frio"
## [241] "Hansford" "Limestone" "Real" "Milam"
## [245] "Uvalde" "Foard" "Kent" "Armstrong"
## [249] "Knox" "Brown" "Clay" "Franklin"
## [253] "Hays" "Llano"
# make simple plot of MD counties using geom_sf
md.map<-ggplot(md.cos)+
geom_sf(fill='blue', color='white')
md.map

md<-md.state%>%st_transform(2248)
md.cos<-md.cos%>%st_transform(2248)
st_crs(md) # units are in us ft
## Coordinate Reference System:
## EPSG: 2248
## proj4string: "+proj=lcc +lat_1=39.45 +lat_2=38.3 +lat_0=37.66666666666666 +lon_0=-77 +x_0=399999.9998983998 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs"
md.pt<-md%>%st_cast('POINT') # turn MD border into points
glimpse(md.cos)
## Observations: 254
## Variables: 13
## $ statefp <chr> "48", "48", "48", "48", "48", "48", "48", "48", "...
## $ countyfp <chr> "175", "185", "215", "225", "235", "273", "277", ...
## $ countyns <chr> "01383873", "01383878", "01383893", "01383898", "...
## $ affgeoid <chr> "0500000US48175", "0500000US48185", "0500000US482...
## $ geoid <chr> "48175", "48185", "48215", "48225", "48235", "482...
## $ name <chr> "Goliad", "Grimes", "Hidalgo", "Houston", "Irion"...
## $ lsad <chr> "06", "06", "06", "06", "06", "06", "06", "06", "...
## $ aland <dbl> 2206698586, 2039539026, 4068610438, 3188288247, 2...
## $ awater <dbl> 19050015, 36603914, 31102594, 14426022, 176317, 5...
## $ state_name <chr> "Texas", "Texas", "Texas", "Texas", "Texas", "Tex...
## $ state_abbr <chr> "TX", "TX", "TX", "TX", "TX", "TX", "TX", "TX", "...
## $ jurisdiction_type <chr> "state", "state", "state", "state", "state", "sta...
## $ geometry <MULTIPOLYGON [US_survey_foot]> MULTIPOLYGON (((-539462...
## Simple feature collection with 254 features and 12 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -7685276 ymin: -3461287 xmax: -3962600 ymax: 546463.1
## epsg (SRID): 2248
## proj4string: +proj=lcc +lat_1=39.45 +lat_2=38.3 +lat_0=37.66666666666666 +lon_0=-77 +x_0=399999.9998983998 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs
## First 10 features:
## statefp countyfp countyns affgeoid geoid name lsad aland
## 126 48 175 01383873 0500000US48175 48175 Goliad 06 2206698586
## 127 48 185 01383878 0500000US48185 48185 Grimes 06 2039539026
## 128 48 215 01383893 0500000US48215 48215 Hidalgo 06 4068610438
## 129 48 225 01383898 0500000US48225 48225 Houston 06 3188288247
## 130 48 235 01383903 0500000US48235 48235 Irion 06 2723485995
## 131 48 273 01383922 0500000US48273 48273 Kleberg 06 2282572442
## 132 48 277 01383925 0500000US48277 48277 Lamar 06 2349622229
## 133 48 317 01383941 0500000US48317 48317 Martin 06 2369724595
## 134 48 351 01383961 0500000US48351 48351 Newton 06 2418212546
## 135 48 361 01383966 0500000US48361 48361 Orange 06 864488646
## awater state_name state_abbr jurisdiction_type
## 126 19050015 Texas TX state
## 127 36603914 Texas TX state
## 128 31102594 Texas TX state
## 129 14426022 Texas TX state
## 130 176317 Texas TX state
## 131 541041664 Texas TX state
## 132 67103153 Texas TX state
## 133 1931832 Texas TX state
## 134 15672146 Texas TX state
## 135 118488848 Texas TX state
## geometry
## 126 POINT (-5283817 -2556221)
## 127 POINT (-4682045 -1977118)
## 128 POINT (-5715739 -3315629)
## 129 POINT (-4449916 -1734960)
## 130 POINT (-6154659 -1336897)
## 131 POINT (-5481126 -2975075)
## 132 POINT (-4321870 -883286.8)
## 133 POINT (-6349809 -902506.6)
## 134 POINT (-3966948 -2028199)
## 135 POINT (-4058219 -2259393)
# use lon & lat for each centroid to place labels
md.cos$lon<-st_coordinates(st_centroid(md.cos))[,1] # add longitude to sf
md.cos$lat<-st_coordinates(st_centroid(md.cos))[,2] # add latitude to sf
# create mape of MD counties colored by land area (aland)
md.co.map<-ggplot()+
geom_sf(data=md, fill=NA, color='black')+
geom_sf(data=md.cos, aes(fill=aland))+
theme_bw()+
scale_fill_viridis(alpha=.5)+
theme(legend.position='none')
md.co.map

# label each county
md.co.map+geom_label_repel(data=md.cos, aes(x=lon, y=lat, label=name))

Louisiana
# data wrangling & plotting
library(tidyverse) # dplyr and ggplot2
library(reshape2)
# spatial analyses - for later
library(sf) # working with vectors - polygons, lines, etc
library(raster) # raster
library(USAboundaries)
library(USAboundariesData)
# color scales
library(viridis)
library(ggrepel)
theme_set(theme_bw())# set plotting theme for session
# get MD boundaries - USA boundaries apckage
md.state<-us_states(resolution = 'high', states='louisiana')
glimpse(md.state) # look at spatial object - basically a dataframe
## Observations: 1
## Variables: 13
## $ statefp <chr> "22"
## $ statens <chr> "01629543"
## $ affgeoid <chr> "0400000US22"
## $ geoid <chr> "22"
## $ stusps <chr> "LA"
## $ name <chr> "Louisiana"
## $ lsad <chr> "00"
## $ aland <dbl> 111904912452
## $ awater <dbl> 23746303848
## $ state_name <chr> "Louisiana"
## $ state_abbr <chr> "LA"
## $ jurisdiction_type <chr> "state"
## $ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-88.8677 29...
# get MD county shapefile
md.cos<-us_counties(resolution = 'high', states='louisiana')
unique(md.cos$name)
## [1] "Ascension" "Beauregard" "Bienville"
## [4] "Bossier" "Catahoula" "Concordia"
## [7] "LaSalle" "Livingston" "Natchitoches"
## [10] "Ouachita" "Rapides" "St. Landry"
## [13] "Vermilion" "Webster" "West Carroll"
## [16] "Sabine" "St. Martin" "St. Mary"
## [19] "Tangipahoa" "Terrebonne" "Vernon"
## [22] "West Feliciana" "East Carroll" "East Feliciana"
## [25] "Franklin" "Iberia" "Lafourche"
## [28] "Madison" "Orleans" "Pointe Coupee"
## [31] "Red River" "Avoyelles" "Caldwell"
## [34] "De Soto" "Jefferson Davis" "Plaquemines"
## [37] "St. Tammany" "Grant" "East Baton Rouge"
## [40] "Acadia" "Caddo" "Evangeline"
## [43] "St. Bernard" "Winn" "St. Helena"
## [46] "Lafayette" "Tensas" "Union"
## [49] "Jefferson" "Morehouse" "Richland"
## [52] "Claiborne" "Jackson" "St. James"
## [55] "Washington" "Iberville" "Allen"
## [58] "Calcasieu" "Cameron" "St. John the Baptist"
## [61] "West Baton Rouge" "Lincoln" "St. Charles"
## [64] "Assumption"
# make simple plot of MD counties using geom_sf
md.map<-ggplot(md.cos)+
geom_sf(fill='blue', color='white')
md.map

md<-md.state%>%st_transform(2248)
md.cos<-md.cos%>%st_transform(2248)
st_crs(md) # units are in us ft
## Coordinate Reference System:
## EPSG: 2248
## proj4string: "+proj=lcc +lat_1=39.45 +lat_2=38.3 +lat_0=37.66666666666666 +lon_0=-77 +x_0=399999.9998983998 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs"
md.pt<-md%>%st_cast('POINT') # turn MD border into points
glimpse(md.cos)
## Observations: 64
## Variables: 13
## $ statefp <chr> "22", "22", "22", "22", "22", "22", "22", "22", "...
## $ countyfp <chr> "005", "011", "013", "015", "025", "029", "059", ...
## $ countyns <chr> "00558403", "00558436", "00558445", "00558453", "...
## $ affgeoid <chr> "0500000US22005", "0500000US22011", "0500000US220...
## $ geoid <chr> "22005", "22011", "22013", "22015", "22025", "220...
## $ name <chr> "Ascension", "Beauregard", "Bienville", "Bossier"...
## $ lsad <chr> "15", "15", "15", "15", "15", "15", "15", "15", "...
## $ aland <dbl> 751029431, 2997502171, 2101207370, 2176181488, 18...
## $ awater <dbl> 33194722, 21999001, 27945994, 70514387, 81308667,...
## $ state_name <chr> "Louisiana", "Louisiana", "Louisiana", "Louisiana...
## $ state_abbr <chr> "LA", "LA", "LA", "LA", "LA", "LA", "LA", "LA", "...
## $ jurisdiction_type <chr> "state", "state", "state", "state", "state", "sta...
## $ geometry <MULTIPOLYGON [US_survey_foot]> MULTIPOLYGON (((-318063...
## Simple feature collection with 64 features and 12 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -3888731 ymin: -2728170 xmax: -2701919 ymax: -1335380
## epsg (SRID): 2248
## proj4string: +proj=lcc +lat_1=39.45 +lat_2=38.3 +lat_0=37.66666666666666 +lon_0=-77 +x_0=399999.9998983998 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs
## First 10 features:
## statefp countyfp countyns affgeoid geoid name lsad aland
## 161 22 005 00558403 0500000US22005 22005 Ascension 15 751029431
## 162 22 011 00558436 0500000US22011 22011 Beauregard 15 2997502171
## 163 22 013 00558445 0500000US22013 22013 Bienville 15 2101207370
## 164 22 015 00558453 0500000US22015 22015 Bossier 15 2176181488
## 165 22 025 00558495 0500000US22025 22025 Catahoula 15 1833782911
## 166 22 029 00558515 0500000US22029 22029 Concordia 15 1805172274
## 167 22 059 00558049 0500000US22059 22059 LaSalle 15 1617924843
## 168 22 063 00558083 0500000US22063 22063 Livingston 15 1678750673
## 169 22 069 00558108 0500000US22069 22069 Natchitoches 15 3243341073
## 170 22 073 00558114 0500000US22073 22073 Ouachita 15 1580943091
## awater state_name state_abbr jurisdiction_type geometry
## 161 33194722 Louisiana LA state POINT (-3113635 -2389615)
## 162 21999001 Louisiana LA state POINT (-3850694 -2100605)
## 163 27945994 Louisiana LA state POINT (-3651795 -1503423)
## 164 70514387 Louisiana LA state POINT (-3797694 -1353845)
## 165 81308667 Louisiana LA state POINT (-3321959 -1813023)
## 166 130329995 Louisiana LA state POINT (-3270787 -1903209)
## 167 97664776 Louisiana LA state POINT (-3418112 -1793096)
## 168 112119167 Louisiana LA state POINT (-3042648 -2312510)
## 169 121033890 Louisiana LA state POINT (-3704174 -1726316)
## 170 55283099 Louisiana LA state POINT (-3367928 -1503722)
# use lon & lat for each centroid to place labels
md.cos$lon<-st_coordinates(st_centroid(md.cos))[,1] # add longitude to sf
md.cos$lat<-st_coordinates(st_centroid(md.cos))[,2] # add latitude to sf
# create mape of MD counties colored by land area (aland)
md.co.map<-ggplot()+
geom_sf(data=md, fill=NA, color='black')+
geom_sf(data=md.cos, aes(fill=aland))+
theme_bw()+
scale_fill_viridis(alpha=.5)+
theme(legend.position='none')
md.co.map

# label each county
md.co.map+geom_label_repel(data=md.cos, aes(x=lon, y=lat, label=name))

California
# data wrangling & plotting
library(tidyverse) # dplyr and ggplot2
library(reshape2)
# spatial analyses - for later
library(sf) # working with vectors - polygons, lines, etc
library(raster) # raster
library(USAboundaries)
library(USAboundariesData)
# color scales
library(viridis)
library(ggrepel)
theme_set(theme_bw())# set plotting theme for session
# get MD boundaries - USA boundaries apckage
md.state<-us_states(resolution = 'high', states='california')
glimpse(md.state) # look at spatial object - basically a dataframe
## Observations: 1
## Variables: 13
## $ statefp <chr> "06"
## $ statens <chr> "01779778"
## $ affgeoid <chr> "0400000US06"
## $ geoid <chr> "06"
## $ stusps <chr> "CA"
## $ name <chr> "California"
## $ lsad <chr> "00"
## $ aland <dbl> 403501101370
## $ awater <dbl> 20466718403
## $ state_name <chr> "California"
## $ state_abbr <chr> "CA"
## $ jurisdiction_type <chr> "state"
## $ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-118.6044 3...
# get MD county shapefile
md.cos<-us_counties(resolution = 'high', states='california')
unique(md.cos$name)
## [1] "Butte" "Calaveras" "Colusa" "El Dorado"
## [5] "Fresno" "Humboldt" "Lake" "Los Angeles"
## [9] "Mendocino" "Monterey" "Santa Barbara" "Sonoma"
## [13] "Yolo" "Marin" "Mariposa" "Mono"
## [17] "Napa" "Nevada" "Placer" "San Bernardino"
## [21] "San Mateo" "Santa Clara" "Shasta" "Sierra"
## [25] "Stanislaus" "Ventura" "Yuba" "Amador"
## [29] "Inyo" "Contra Costa" "Orange" "Tulare"
## [33] "Alpine" "Del Norte" "Imperial" "Madera"
## [37] "San Benito" "Santa Cruz" "Alameda" "San Luis Obispo"
## [41] "Riverside" "Merced" "San Joaquin" "Trinity"
## [45] "Sacramento" "Tuolumne" "Glenn" "Kern"
## [49] "Lassen" "Modoc" "Plumas" "Siskiyou"
## [53] "Tehama" "San Francisco" "Solano" "Sutter"
## [57] "San Diego" "Kings"
# make simple plot of MD counties using geom_sf
md.map<-ggplot(md.cos)+
geom_sf(fill='blue', color='white')
md.map

md<-md.state%>%st_transform(2248)
md.cos<-md.cos%>%st_transform(2248)
st_crs(md) # units are in us ft
## Coordinate Reference System:
## EPSG: 2248
## proj4string: "+proj=lcc +lat_1=39.45 +lat_2=38.3 +lat_0=37.66666666666666 +lon_0=-77 +x_0=399999.9998983998 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs"
md.pt<-md%>%st_cast('POINT') # turn MD border into points
glimpse(md.cos)
## Observations: 58
## Variables: 13
## $ statefp <chr> "06", "06", "06", "06", "06", "06", "06", "06", "...
## $ countyfp <chr> "007", "009", "011", "017", "019", "023", "033", ...
## $ countyns <chr> "01675842", "01675885", "01675902", "00277273", "...
## $ affgeoid <chr> "0500000US06007", "0500000US06009", "0500000US060...
## $ geoid <chr> "06007", "06009", "06011", "06017", "06019", "060...
## $ name <chr> "Butte", "Calaveras", "Colusa", "El Dorado", "Fre...
## $ lsad <chr> "06", "06", "06", "06", "06", "06", "06", "06", "...
## $ aland <dbl> 4238423343, 2641820834, 2980372757, 4423349463, 1...
## $ awater <dbl> 105325812, 43806026, 14581043, 203269403, 1353744...
## $ state_name <chr> "California", "California", "California", "Califo...
## $ state_abbr <chr> "CA", "CA", "CA", "CA", "CA", "CA", "CA", "CA", "...
## $ jurisdiction_type <chr> "state", "state", "state", "state", "state", "sta...
## $ geometry <MULTIPOLYGON [US_survey_foot]> MULTIPOLYGON (((-108365...
## Simple feature collection with 58 features and 12 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -11320610 ymin: 759009.5 xmax: -10113210 ymax: 4704993
## epsg (SRID): 2248
## proj4string: +proj=lcc +lat_1=39.45 +lat_2=38.3 +lat_0=37.66666666666666 +lon_0=-77 +x_0=399999.9998983998 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs
## First 10 features:
## statefp countyfp countyns affgeoid geoid name lsad aland
## 45 06 007 01675842 0500000US06007 06007 Butte 06 4238423343
## 46 06 009 01675885 0500000US06009 06009 Calaveras 06 2641820834
## 47 06 011 01675902 0500000US06011 06011 Colusa 06 2980372757
## 48 06 017 00277273 0500000US06017 06017 El Dorado 06 4423349463
## 49 06 019 00277274 0500000US06019 06019 Fresno 06 15433177265
## 50 06 023 01681908 0500000US06023 06023 Humboldt 06 9240992572
## 51 06 033 00277281 0500000US06033 06033 Lake 06 3254288286
## 52 06 037 00277283 0500000US06037 06037 Los Angeles 06 10510651024
## 53 06 045 00277287 0500000US06045 06045 Mendocino 06 9081421729
## 54 06 053 00277291 0500000US06053 06053 Monterey 06 8496669426
## awater state_name state_abbr jurisdiction_type geometry
## 45 105325812 California CA state POINT (-10750644 3735237)
## 46 43806026 California CA state POINT (-10734365 3125440)
## 47 14581043 California CA state POINT (-10993170 3663076)
## 48 203269403 California CA state POINT (-10630791 3307414)
## 49 135374444 California CA state POINT (-10740995 2537461)
## 50 1254297982 California CA state POINT (-11127994 4370091)
## 51 188912510 California CA state POINT (-11135348 3707949)
## 52 1794730436 California CA state POINT (-10753760 1551549)
## 53 962951511 California CA state POINT (-11233778 3903717)
## 54 1270747469 California CA state POINT (-11245490 2575450)
# use lon & lat for each centroid to place labels
md.cos$lon<-st_coordinates(st_centroid(md.cos))[,1] # add longitude to sf
md.cos$lat<-st_coordinates(st_centroid(md.cos))[,2] # add latitude to sf
# create mape of MD counties colored by land area (aland)
md.co.map<-ggplot()+
geom_sf(data=md, fill=NA, color='black')+
geom_sf(data=md.cos, aes(fill=aland))+
theme_bw()+
scale_fill_viridis(alpha=.5)+
theme(legend.position='none')
md.co.map

# label each county
md.co.map+geom_label_repel(data=md.cos, aes(x=lon, y=lat, label=name))

Centroid
library(rjson)
library(DT)
## Warning: package 'DT' was built under R version 3.6.2
doc1<-fromJSON(file="https://raw.githubusercontent.com/CDCgov/MicrobeTrace/master/data/counties.json", method="C")
county_centroid <- do.call(rbind, lapply(doc1$features, function(x) data.frame(x$properties)))
datatable(
county_centroid, extensions = c('Select', 'Buttons'), options = list(
select = list(style = 'os', items = 'row'),
dom = 'Blfrtip',
rowId = 0,
buttons = c('csv', 'excel')
),
selection = 'none'
)