[英]R - Aggregate county map polygons to create custom borders
I can create a county map using ggplot. 我可以使用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. 只需更改ggplot中的组就不起作用了。 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))
You can use gUnaryUnion
from library(rgeos)
to merge polygons, but as you use maps from library maps
, this requires few steps: 您可以使用
gUnaryUnion
从library(rgeos)
合并的多边形,但是当你使用来自图书馆的地图maps
,这需要几个步骤:
get_map
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)
library(sp)
library(sp)
SpatialPolygonsDataFrame 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)
gUnaryUnion
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. 由于库
maps
在拓扑方面不是很干净,因此您需要使用缓冲区来清除它。
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)
Use fortify
to be able to plot the spatialdata with ggplot 使用
fortify
可以用ggplot绘制spatialdata
county.fortify <- fortify(county.region)
ggplot(data = county.fortify) +
geom_polygon(aes(x=long, y=lat, fill=group)) +
guides(fill = FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.