Created
May 12, 2022 16:01
-
-
Save ryanscharf/0fc536c756d7cec2a5f212e11b0bdb78 to your computer and use it in GitHub Desktop.
plot us w/AK& HI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Plot US | |
#' | |
#' @param spdf sf data frame | |
#' @param fill_val quoted fill value name | |
#' @param title plot title | |
#' @param palette color palette | |
#' @param ... idk how these work still | |
#' | |
#' @return | |
#' @export | |
#' | |
#' @examples df %>% left_join(tigris::counties(cb = T) %>% filter(!STATEFP %in% c('78','72','69','66','60' )) , by = c('state' = 'STATE_NAME', 'county' = 'NAMELSAD')) %>% st_sf() %>% plot_us(.,'AAL', 'AAL by State', palette = '-viridis') | |
plot_us <- function(spdf, fill_val, title = '', palette = 'viridis', state_name = 'State' , ...){ | |
place_geometry <- function(geometry, bb, scale_x, scale_y, | |
scale_size = 1) { | |
output_geometry <- (geometry - st_centroid(st_union(geometry))) * scale_size + | |
st_sfc(st_point(c( | |
bb$xmin + scale_x * (bb$xmax - bb$xmin), | |
bb$ymin + scale_y * (bb$ymax - bb$ymin) | |
))) | |
return(output_geometry) | |
} | |
spdf <- spdf %>% mutate( | |
{{state_name}} := toupper(!!sym(state_name)) | |
) | |
# projections ------------------------------------------------------------- | |
# ESRI:102003 https://epsg.io/102003 | |
crs_lower48 <- "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs" | |
# EPSG:3338 https://epsg.io/3338 | |
crs_alaska <- "+proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs " | |
# ESRI:102007 https://epsg.io/102007 | |
crs_hawaii <- "+proj=aea +lat_1=8 +lat_2=18 +lat_0=13 +lon_0=-157 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs" | |
# prepare us_lower 48 ----------------------------------------------------- | |
us_lower48 <- spdf %>% | |
filter(!(!!sym(state_name) %in% c('ALASKA', 'HAWAII')) | |
) %>% | |
st_transform(crs_lower48) | |
# prepare alaska ---------------------------------------------------------- | |
alaska2 <- spdf %>% | |
filter(!!sym(state_name) == 'ALASKA') %>% | |
st_transform(crs_alaska) %>% | |
mutate(geometry = place_geometry(geometry, st_bbox(us_lower48), 0.6, 1.35)) %>% | |
st_set_crs(crs_lower48) | |
if( | |
alaska2 %>% | |
filter(!is.na(!!ensym(fill_val))) %>% nrow() == 0 | |
){ | |
alaska2 <- alaska2 %>% | |
filter(!is.na(!!ensym(fill_val))) | |
} | |
# prepare hawaii ---------------------------------------------------------- | |
hawaii2 <- spdf %>% | |
filter(!!sym(state_name) == 'HAWAII') %>% | |
st_transform(crs_hawaii) %>% | |
mutate(geometry = place_geometry(geometry, st_bbox(us_lower48), 0.2, 0.1)) %>% | |
st_set_crs(crs_lower48) | |
if( | |
hawaii2 %>% | |
filter(!is.na(!!ensym(fill_val))) %>% nrow() == 0 | |
){ | |
hawaii2 <- hawaii2 %>% | |
filter(!is.na(!!ensym(fill_val))) | |
} | |
# combine data ------------------------------------------------------------ | |
us_albers_alt <- rbind(us_lower48, alaska2, hawaii2) | |
# plot -------------------------------------------------------------------- | |
tm_shape(us_albers_alt) + | |
tm_polygons(col = fill_val, | |
border.col = "black", | |
lwd = 0.5, | |
style = "cont", | |
palette = palette, | |
legend.reverse = T) + | |
tm_layout(title) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment