2017-10-05 96 views
3

我想將shapefile應用於ggmaps貼圖,但它給了我很奇怪的結果。所討論的shapefile是澳大利亞統計局提供的「統計局域」(類似於郵編的組)形狀文件,可用於hereshapefile中的奇怪多邊形

SLA Shapefile applied at zoom level 4

通常我會認爲這是切斷邊緣點的問題,但我打它,即使在縮放級別1(其實它看起來更糟): SLA Shapefile applied at zoom level 1

這裏的一些代碼我用來產生上面的圖表:

library(tidyverse) 
library(ggmap) 
library(rgdal) 

slas <- readOGR(dsn="SLA",layer="SLA11aAust") 

aus4 <- get_map("Australia",zoom=4) 
ggmap(aus4) 

ggmap(aus4)+ 
    geom_polygon(data=slas, aes(x=long,y=lat)) 

aus1 <- get_map("Australia",zoom=1) 
ggmap(aus1) 

ggmap(aus1)+ 
    geom_polygon(data=slas, aes(x=long,y=lat)) 

我做錯了什麼,或者是shapefile以某種方式錯誤配置?

回答

6

我想你只需要(可選)fortify變量slas,不要忘了group,使邊界與彩色可視:

slas <- fortify(slas, region = "SLA_CODE11") 
ggmap(aus4) + 
    geom_polygon(data = slas2, color = "white", aes(x = long, y = lat, group = group)) 

enter image description here

+0

你說得對,那看起來好多了。什麼'fortify'呢?該文件現在只是說「去使用'掃帚'包,而不是」,這是沒有用的。 – Margaret

+2

'fortify'將'slas'從'SpatialPolygonsDataFrame'轉換爲常規'data.frame'。嚴格地講,這不是必要的;缺少的關鍵是'group = group'參數。 – neilfws

相關問題