Paulp8
Paulp8

Reputation: 31

R - Aggregate county map polygons to create custom borders

I can create a county map using ggplot. I would like to use this map data, and map new boundaries that are aggregates of counties. For example, an equivalent task to mine would be to map state boundaries ("region" variable), rather than county boundaries, using this county map dataset (I have another variable I would use to aggregate counties).

Simply changing the group in ggplot does not work for this. I believe I might need to create new polygon shapes based on the variable I am using to aggregate, but I am not sure. Any thoughts on how I can do this? Any help is greatly appreciated!

library(ggplot2)

# load county map data
m.county <- map_data("county")
head(m.county)
       long      lat group order  region subregion
1 -86.50517 32.34920     1     1 alabama   autauga
2 -86.53382 32.35493     1     2 alabama   autauga
3 -86.54527 32.36639     1     3 alabama   autauga
4 -86.55673 32.37785     1     4 alabama   autauga
5 -86.57966 32.38357     1     5 alabama   autauga
6 -86.59111 32.37785     1     6 alabama   autauga

# map county data with county borders
ggplot(data = m.county) +
  geom_polygon(aes(x=long, y=lat,group=group)) 

Upvotes: 3

Views: 1638

Answers (1)

S&#233;bastien Rochette
S&#233;bastien Rochette

Reputation: 6661

You can use gUnaryUnion from library(rgeos) to merge polygons, but as you use maps from library maps, this requires few steps:

Get the data from map library, which is what uses get_map

library(ggplot2)
library(sp)
library(rgdal)
library(maps)
library(mapdata)

# adapted from 
# http://stackoverflow.com/questions/26062280/converting-a-map-object-to-a-spatialpolygon-object
require(sp)
require(maptools)
county <- map("county", fill = TRUE)

Transform map data into SpatialPolygonsDataFrame of library(sp)

head(county$names)
county.sp <- map2SpatialPolygons(county, IDs = as.factor(county$names), 
                                 proj4string = CRS("+proj=longlat +datum=WGS84"))

# Add information data of the polygons
region <- sapply(strsplit(county$names, ","), function(x) x[1])
subregion <- sapply(strsplit(county$names, ","), function(x) x[2])
subregion[is.na(subregion)] <- region[is.na(subregion)]

# Create the SpatialPolygonsDataFrame
county.sp.data <- SpatialPolygonsDataFrame(
  county.sp, 
  data = data.frame(region = region,
                    subregion = subregion),
  match.ID = FALSE)

Merge polygons according to region using gUnaryUnion

Because maps from library maps is not clean in terms of topology, you need to use a buffer as a trick to clean it.

library(rgeos)
# Because of topology problems
county.sp.data.buffer <- gBuffer(county.sp.data, byid = TRUE, width = 0)
# Merge polygons according to region
county.region <- gUnaryUnion(county.sp.data.buffer, 
                             id = county.sp.data.buffer@data$region)

Transform back as a dataset suitable for ggplot

Use fortify to be able to plot the spatialdata with ggplot

county.fortify <- fortify(county.region)

ggplot(data = county.fortify) +
  geom_polygon(aes(x=long, y=lat, fill=group)) +
  guides(fill = FALSE)

Upvotes: 1

Related Questions