ggmap:从shapefile绘制多边形


9

使用ggmap,我想在包含一些位置点的地图上包括来自shapefile的市政边界(多边形)。除了绘制多边形外,此脚本会执行所有操作:

library(rgdal)
library(ggmap)

# Get shapefile with Drammen municipality borders
tmpzip<-tempfile()
tmpdir<-tempfile()
dir.create(tmpdir)
download.file("http://www.kartverket.no/Documents/Kart/N50-N5000%20Kartdata/33_N5000_shape.zip",tmpzip)
unzip(tmpzip, exdir=tmpdir)
kommune <- readOGR(dsn=tmpdir, layer="NO_AdminOmrader_pol")
kommune<-kommune[kommune$NAVN=="Drammen",]
kommune<-spTransform(kommune, CRS("+init=epsg:4326"))

# Get location point data 
subscr<-data.frame(lon=c(10.1237,10.2161,10.2993),lat=c(59.7567,59.7527,59.6863), pop=c(58,12,150))
coordinates(subscr)<-~lon+lat
proj4string(subscr)<-CRS("+init=epsg:4326")

lon <- c(10.0937,10.3293)
lat <- c(59.7916,59.6563)
map <- get_map(location = c(lon[1], lat[2], lon[2], lat[1]),
               maptype = "roadmap", source = "osm", zoom = 11)
p <- ggmap(map) +
  geom_point(data = as.data.frame(subscr), aes(x = lon, y = lat, size=pop),
             colour = "darkgreen") +
  theme_bw()
print(p)

如何从shapefile中绘制多边形?我尝试用以下内容替换第二行:

p <- ggmap(map) +
  geom_point(data = as.data.frame(subscr), aes(x = lon, y = lat, size=pop),
             colour = "darkgreen") +
  geom_polygon(data = as.data.frame(kommune)) +
  theme_bw()

但是然后我得到以下错误:

Error: Aesthetics must be either length 1 or the same as the data (1): x, y

Answers:


9

as.data.frame()不适用于SpatialPolgonsin geom_polygon,因为几何体丢失了。您必须使用ggplot2::fortify(将来可能不推荐使用,请参阅?fortify)。现在推荐的方法是使用broom::tidy

R> library("broom")
R> head(tidy(kommune))
Regions defined for each Polygons
   long   lat order  hole piece group  id
1 10.29 59.72     1 FALSE     1 153.1 153
2 10.32 59.70     2 FALSE     1 153.1 153
3 10.32 59.69     3 FALSE     1 153.1 153
4 10.31 59.68     4 FALSE     1 153.1 153
5 10.30 59.67     5 FALSE     1 153.1 153
6 10.28 59.67     6 FALSE     1 153.1 153

但是,您的示例还会出现另一个问题。由于多边形大于地图范围,因此ggmap无法正确裁剪多边形。 ggmap设置规模限制,这将丢弃所有不在这些限制之内的数据。

这是您的代码的修改后的版本:

p <- ggmap(map, extent = "normal", maprange = FALSE) +
     geom_point(data = as.data.frame(subscr),
                aes(x = lon, y = lat, size=pop),
                colour = "darkgreen") +
     geom_polygon(data = fortify(kommune),
                  aes(long, lat, group = group),
                  fill = "orange", colour = "red", alpha = 0.2) +
     theme_bw() +
     coord_map(projection="mercator",
               xlim=c(attr(map, "bb")$ll.lon, attr(map, "bb")$ur.lon),
               ylim=c(attr(map, "bb")$ll.lat, attr(map, "bb")$ur.lat))

print(p)

ggmap


你又救了我一天!
matthiash

2

要添加到上面的答案中:对于那些遵循了出色的教程/答案并想知道如何解决有关多边形裁剪的下一个问题的人(就像我一样!)

这是在/programming/13982773/crop-for-spatialpolygonsdataframe上由用户'streamlinedmethod'提供的答案

library(maptools)
library(raster)   ## To convert an "Extent" object to a "SpatialPolygons" object.
library(raster)   ## To convert an "Extent" object to a "SpatialPolygons" object.
library(rgeos)
data(wrld_simpl)

# Create the clipping polygon
CP <- as(extent(130, 180, 40, 70), "SpatialPolygons")
proj4string(CP) <- CRS(proj4string(wrld_simpl))

# Clip the map
out <- gIntersection(wrld_simpl, CP, byid=TRUE)

那么当您进行规划时,就不会出现奇怪的裁剪问题。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.