How do I use ggplot2 to create a border around a group of US counties?

后端 未结 1 811
暖寄归人
暖寄归人 2021-01-22 10:28

I am relatively new to using R and I am trying to create maps of US states using data to outline and colour certain areas. I am trying to display a state and its counties outlin

相关标签:
1条回答
  • 2021-01-22 11:16

    Edited answer based on OP's clarification to only colour the outer borders of each rating area:

    As far as I understand it, all polygons are created equal where ggplot is concerned. Hence if it has to colour the outline of a polygon, it will do so for all sides, regardless whether two adjacent polygons belong to the same rating area.

    You'll have the dissolve polygons in the same planning area, before fortifying them into a data frame. (Note: you can convert the existing data frame back to polygon, but it's probably easier to get polygon data from the original data source.)

    library(maps); library(dplyr); library(tidyr); library(maptools); library(rgeos)
    
    # get map data
    county_map <- map("county", fill = T, plot = FALSE)
    
    # create mapping table between county names & rating areas
    county_map_match <- data.frame(name = county_map$names) %>%
      separate(name, c("region", "subregion"), sep = ",", remove = FALSE) %>%
      left_join(plan %>% select(region, subregion, RatingArea))
    rownames(county_map_match) <- county_map_match$name
    
    # convert map to SpatialPolygon, then join with mapping table for SpatialPolygonDataFrame
    county_map <- map2SpatialPolygons(county_map, IDs = county_map$names)
    county_map <- SpatialPolygonsDataFrame(county_map, county_map_match)
    
    # remove invalidities in the county map
    gIsValid(county_map) #returns FALSE: there are invalid self-intersecting geometries in the polygons, which will cause problems
    county_map <- gBuffer(county_map, byid = TRUE, width = 0)
    gIsValid(county_map) #returns TRUE
    
    # dissolve county map by rating area & fortify to data frame
    area_map <- unionSpatialPolygons(county_map, IDs = county_map$RatingArea)
    area_map <- fortify(area_map)
    area_map$group <- gsub(".1", "", x= area_map$group, fixed = T)
    

    Once you obtain the data frame version for rating areas, you can incorporate it into the ggplot:

    ggplot(countyplan,
           aes(x=long,y=lat, group = group, fill = chosen_plan)) + 
      geom_polygon(size = 0.5, colour = "black") +
      geom_polygon(data = area_map, 
                aes(x=long, y=lat, group = group, colour = group), 
                fill = NA, size = 2) +
      scale_fill_manual(name = "Chosen Plan", values = c("darksalmon"), na.value = "grey") +
      scale_color_discrete(name = "Rating Area") +
      coord_map() + coord_fixed(1.3)
    

    You can get nicer color palettes from the RColorBrewer package & use them in the scale_XX_brewer() call, if you like. Names for individual colors can be referenced here: http://sape.inf.usi.ch/quick-reference/ggplot2/colour

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