Removing the Great Lakes from US county-level maps in R

前端 未结 1 1488
北恋
北恋 2021-01-20 10:37

I am using R to draw US map at county level. I downloaded the shapefile for US from GADM. The county-level shape file is "gadm36_USA_2.shp". I then used the code b

1条回答
  •  借酒劲吻你
    2021-01-20 11:25

    One way is to remove every feature that is tagged with Lake in the existing records (currently 13 features). First, you need to find the lakes name in the attribute table as below:

    # retrieving the name of lakes and excluding them from the sf 
    
    all.names = us2$NAME_2
    patterns = c("Lake", "lake")
    
    lakes.name <- unique(grep(paste(patterns, collapse="|"), all.names, value=TRUE, ignore.case = TRUE))
    #[1] "Lake and Peninsula" "Lake"               "Bear Lake"          "Lake Michigan"      "Lake Hurron"        "Lake St. Clair"    
    #[7] "Lake Superior"      "Lake of the Woods"  "Red Lake"           "Lake Ontario"       "Lake Erie"          "Salt Lake"         
    #[13] "Green Lake" 
    
    `%notin%` <- Negate(`%in%`)
    us <- us2[us2$NAME_2 %notin% lakes.name, ]
    

    Then you can map the remaining features:

    mainland2 <- ggplot(data = us) +
      geom_sf(aes(fill = NAME_2), size = 0.4, color = "black") +
      coord_sf(crs = st_crs(2163), 
               xlim = c(-2500000, 2500000), 
               ylim = c(-2300000, 730000)) + guides(fill = F)
    mainland2
    

    Another way (much easier but less flexible) is to map county features by excluding Water body values from ENGTYPE_2 as below:

    us <- us2[(us2$ENGTYPE_2) != "Water body",]
    mainland2 <- ggplot(data = us) +
      geom_sf(aes(fill = NAME_2), size = 0.4, color = "black") +
      coord_sf(crs = st_crs(2163), 
               xlim = c(-2500000, 2500000), 
               ylim = c(-2300000, 730000)) + guides(fill = F)
    mainland2
    

    0 讨论(0)
提交回复
热议问题