2015-04-23 42 views
6

我在尝试将ggmap与形状文件结合时出现剪裁问题。 Kahle和Wickham的例子(2013:158)正常工作,因为来自ggmap的栅格图像覆盖整个形状文件。下面是一个例子,当我尝试在一个覆盖更小区域的ggmap图上绘制美国州的形状文件时会发生什么。 ggmap显示了纽约市,我想用美国各州的边界覆盖它(就像一个例子)。由此产生的地图没有任何意义。问题是形状文件被剪切,ggplot连接未剪切的点。以下是代码。形状文件从here。我只是在这里展示最后的情节。使用ggmap绘制形状文件:当形状文件大于ggmap时剪裁

我怎样才能解决这个问题?

path <- "PATH TO SHAPEFILE" 
library("ggmap") 
library("rgdal") 

# shapefile 
states <- readOGR(dsn = path, layer = "states") 
states_df <- fortify(states) 
# plot shapefile 
plot(states, lwd = 0.1) 
ggplot(states_df, aes(long, lat, group = group)) + 
    geom_polygon(colour = "black", fill = NA, size = 0.1) 


# combine ggmap with shapefile 
map <- get_map("new york city", zoom = 10, source = "stamen") 
ggmap(map, extent = "device") 

ggmap(map, extent = "device") + 
    geom_polygon(aes(long, lat, group=group), data = states_df, colour = "red", fill = NA, size = 1) 

Kahle,David和Hadley Wickham。 “Ggmap:使用ggplot2进行空间可视化”,The R Journal 5(1):144-61。

enter image description here

+0

与此有关的问题在这里的问题:http://stackoverflow.com/questions/13469566/polygons-nicely-cropping-ggplot2-ggmap-at-different-zoom-levels? – user1965813

回答

3

这里是我的尝试。我经常使用GADM shapefile,您可以使用raster包直接导入。我将NY,NJ和CT的形状文件进行了子集划分。最终你可能不需要这样做,但减少数据量可能会更好。当我绘制地图时,ggplot会自动删除停留在ggmap图像的bbox之外的数据点。因此,我不必做任何额外的工作。我不确定你使用了哪个shapefile。但是,GADM的数据似乎与ggmap图像一起工作良好。希望这可以帮助你。

library(raster) 
library(rgdal) 
library(rgeos) 
library(ggplot2) 

### Get data (shapefile) 
us <- getData("GADM", country = "US", level = 1) 

### Select NY and NJ 
states <- subset(us, NAME_1 %in% c("New York", "New Jersey", "Connecticut")) 

### SPDF to DF 
map <- fortify(states) 

## Get a map 
mymap <- get_map("new york city", zoom = 10, source = "stamen") 


ggmap(mymap) + 
geom_map(data = map, map = map, aes(x = long, y = lat, map_id = id, group = group)) 

enter image description here

如果你只想线,下面将是你所追求的。

enter image description here

+0

不错的结果,你将要碰到的使用geom_path作为多边形的一个问题是,如果形状文件中的顶点数量不够高,那么将会在地图边缘附近进行截断。 –

+0

这是否解决了截断问题ggmap(mymap)+ geom_map(data = map,map = map,aes(x = long,y = lat,map_id = id,group = group),color =“black”,fill = NA,size = 0.5)'?我在边界线的顶端中间看到了问题,但似乎并不是'geom_map'和'fill = NA'的情况。 – user2503795

+0

@ user2503795看起来你的代码运行良好。我为你感到高兴。 :) – jazzurro

2

我想看看这个答案,似乎ggmap如你预期不处理多边形以理想的方式,当你放大,即项目不是在阴谋被截断造成“有趣”的结果关于形状文件。

Polygons nicely cropping ggplot2/ggmap at different zoom levels

# transform for good measure 
states <- spTransform(states,CRS("+datum=WGS84 +proj=longlat")) 

# combine ggmap with shapefile 
states_df <- fortify(states) 

# get your map 
map <-get_map("new york city", zoom = 10, source = "stamen") 

a <- ggmap(map, # this is where we get our raster 
     base_layer=ggplot(aes(x=long, y=lat), data=states_df), # this defines the region where things are plotted 
     extent = "normal", # this won't work with device, you need normal (see examples in ggmap documentation) 
     maprange=FALSE 
     ) + 
coord_map(# use map's bounding box to setup the 'viewport' we want to see 
    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) 
) + 
geom_polygon(# plot the polygon 
    aes(x=long, y=lat,group=group), data =states_df, color = "red", fill=NA, size = 1) 

print(a) 

随着输出: enter image description here

作为一个侧面说明,你可能想看看使用状态地图中的美国人口普查数据,他们似乎比ESRI具有更高的质量数据集。

ftp://ftp2.census.gov/geo/pvs/tiger2010st/tl_2010_us_state10.zip

最后一点,有与两极附近ggmap问题,所以我也由各州子集你的数据,你有兴趣。