Search code examples
rmapsshapefiletigris

Is it possible to condense a county-level map of the US, including Alaska and Hawaii, using certain shapefiles?


I am working on a project that requires me to map out all counties for all 50 states of the US. So far I was partially successful in mapping out my data. The issue I am running into is that with my code, Alaska and Hawaii are extremely spread out using the shapefiles I have currently.

Here is my code:

df <- tigris::counties(state = NULL, cb = TRUE, class = 'sf')
# this step is to remove all of the polygons for US territories 
# since my data does not include it
df <- df %>% filter(!STATEFP %in% c('60', '66', '69', '72', '78'))
ggplot(df) + geom_sf()

And it creates this photo:

map of 50 US states

As you can see, the tail end of Alaska is being plotted on the right side of the graph, making my entire map stretch. Is there another way to better plot Alaska and Hawaii, or other shapefiles that I should consider using to make my map more condensed?


Solution

  • I did that following Quentin Read's blog. My code was the following:

    df <- tigris::counties(state = NULL, cb = T, class = 'sf')
    
    df <- df %>% filter(!STATEFP %in% c('60', '66', '69', '72', '78'))
    
    crs_lambert <- "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"
    
    df <- df %>%
      st_transform(crs = crs_lambert)
    
    
    alaska <- df %>% filter(STATE_NAME %in% 'Alaska')
    alaska_g <- st_geometry(alaska)
    alaska_centroid <- st_centroid(st_union(alaska_g))
    
    rot <- function(a) matrix(c(cos(a), sin(a), -sin(a), cos(a)), 2, 2)
    
    alaska_trans <- (alaska_g - alaska_centroid) * rot(-39 * pi/180) / 2.3 + alaska_centroid + c(1000000, -5000000)
    
    alaska <- alaska %>% st_set_geometry(alaska_trans) %>% st_set_crs(st_crs(df))
    
    
    hawaii <- df %>% filter(STATE_NAME %in% 'Hawaii')
    
    hawaii_g <- st_geometry(hawaii)
    hawaii_centroid <- st_centroid(st_union(hawaii_g))
    
    hawaii_trans <- (hawaii_g - hawaii_centroid) * rot(-35 * pi/180) + hawaii_centroid + c(5200000, -1400000)
    hawaii <- hawaii %>% st_set_geometry(hawaii_trans) %>% st_set_crs(st_crs(df))
    
    tnc_map_final <- df %>%
      filter(!STATE_NAME %in% c('Alaska', 'Hawaii')) %>%
      rbind(alaska) %>%
      rbind(hawaii)
    
    ggplot(tnc_map_final) + 
      geom_sf()
    

    As a result, I got the following map. enter image description here