1
我試圖在通過ggmap生成的地圖上繪製幾個shapefile。這是行之有效的,但是我想限制視圖區域的形狀文件(並不依賴zoom
參數ggmaps
)。我已經通過獲取邊界框並將其作爲參數傳遞給ggplot
的coord_cartesian
來完成這項工作,但我在地圖邊緣發現了一些撕裂問題 - 特別是在西部地區。我嘗試過手動調整x-y座標,但它似乎只會嚴重扭曲圖片。在ggmap上繪製Shapefile
我的想法是,以縮小稍微使整個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]))
當你使用'coord_map()'會發生什麼? –