2016-08-04 52 views
1

我试图在通过ggmap生成的地图上绘制几个shapefile。这是行之有效的,但是我想限制视图区域的形状文件(并不依赖zoom参数ggmaps)。我已经通过获取边界框并将其作为参数传递给ggplotcoord_cartesian来完成这项工作,但我在地图边缘发现了一些撕裂问题 - 特别是在西部地区。我尝试过手动调整x-y坐标,但它似乎只会严重扭曲图片。在ggmap上绘制Shapefile

Detroit Shapefile Map

我的想法是,以缩小稍微使整个shape文件在该地区的绘制,但我似乎无法弄清楚。我也可能完全以错误的方式来解决这个问题。

下面是我用来生成地图的代码。该shapefile可以下载here

library(dplyr) 
library(ggmap) 
library(rgdal) 
library(broom) 

# Read in shapefile, project into long-lat 
# Create 'tbox' which is a minimum bounding box around the shapefile 

tracts <- readOGR(dsn = ".", layer = "CensusTracts2010") %>% 
    spTransform("+proj=longlat +ellps=WGS84") 
tbox <- bbox(tracts) 

# Plot data 
tract_plot <- tidy(tracts) 

DetroitMap <- qmap("Detroit", zoom = 11) 

DetroitMap + geom_polygon(data = tract_plot, aes(x = long, y = lat, group = id), color = "black", fill = NA) + 
    coord_cartesian(xlim = c(tbox[1,1], tbox[1,2]), 
        ylim = c(tbox[2,1], tbox[2,2])) 
+0

当你使用'coord_map()'会发生什么? –

回答

0

我跟着你的工作流程,这导致了与你上面提到的相同的问题。然后,我改变了变焦的qmap选项,从11到10,这导致了更好的图片,虽然你失去了一些地名,但你可以自己手动与annotate添加这些:

DetroitMap <- qmap("Detroit", zoom = 10) 

DetroitMap + geom_polygon(data = tract_plot, aes(x = long, y = lat, group = id), color = "black", fill = NA) + 
    coord_cartesian(xlim = c(tbox[1,1], tbox[1,2]), 
        ylim = c(tbox[2,1], tbox[2,2])) 

enter image description here