2016-08-04 54 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