I love using sf for shapefiles BUT I'm doing some work in a Census data center and need to make some maps where GDAL isn't up to date to be able to use sf. Updating GDAL isn't an option so I want to do the following:
- Import shapefiles using sf on my computer
- Create data.frames which have group, order, region, and subregion to be used with geom_polygon
- Save the data.frames and get it uploaded to the Census data center to use in a script like the one below. I can't use the built-in maps package definition of counties because they have it wrong in VA as shown in this old post: data - Is there a county-level maps dataset that includes independent cities in virginia? - Geographic Information Systems Stack Exchange on SO
library(ggplot2)
library(tibble)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(magrittr)
library(tidyr)
#>
#> Attaching package: 'tidyr'
#> The following object is masked from 'package:magrittr':
#>
#> extract
library(purrr)
#>
#> Attaching package: 'purrr'
#> The following object is masked from 'package:magrittr':
#>
#> set_names
library(stringr)
library(maps)
#>
#> Attaching package: 'maps'
#> The following object is masked from 'package:purrr':
#>
#> map
# https://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html
data(county.fips) #this is a crosswalk between state/county names and FIPS
#make a dataframe of the polygon information
map_county_df <- map_data("county") %>%
mutate(polyname=str_c(region, subregion, sep=","))
#Add on FIPS code for later merges
#Some merging was not possible and manually looked up
map_county_df_fips <- map_county_df %>%
left_join(county.fips, by="polyname") %>%
mutate(char_fips=case_when(
!is.na(fips)~str_pad(fips, 5, side="left", pad="0"),
str_detect(polyname, "okaloosa")~"12091", #manually looked these up
str_detect(polyname, "st martin")~"22099",
str_detect(polyname, "currituck")~"37053",
str_detect(polyname, "galveston")~"48167",
str_detect(polyname, "accomack")~"51001",
str_detect(polyname, "pierce")~ "53053",
str_detect(polyname, "san juan")~ "53055"
))
#Ensure every thing has a FIPS code
map_county_df_fips %>%
filter(is.na(char_fips)) %>%
select(region, subregion, polyname)
#> [1] region subregion polyname
#> <0 rows> (or 0-length row.names)
#Don't need axes for maps so making this to do that
ditch_the_axes <- theme(
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank()
)
#In RDC, need to merge on variables and then fill by that thing.
map_county_df_fips %>%
filter(str_sub(char_fips, 1, 2)=="51") %>%
ggplot(aes(x = long, y = lat, group = group, fill=subregion)) +
geom_polygon(colour="black") +
coord_fixed(1.3) +
guides(fill=FALSE) +
ditch_the_axes
Created on 2019-11-10 by the reprex package (v0.3.0)