2014-11-22 33 views
2

在試圖從人口普查局2013年shapefile中繪製美國縣時,結果繪圖出現了漏洞/怪異填充,尤其是在具有不連續區域的縣附近。我認爲這與繪製點的順序有關,但我不確定它如何修復。例如,佛羅里達的曲線圖,特別是圍繞密鑰產生:使用ggplot2繪製形狀文件中的文物

library(ggplot2) 
library(dplyr) 
library(rgdal) 

download.file('http://www2.census.gov/geo/tiger/GENZ2013/cb_2013_us_county_500k.zip', 
'county.zip') 
unzip('county.zip') 

uscounties <- readOGR(.,'cb_2013_us_county_500k') 
[email protected]$id <- rownames([email protected]) 
countypoints <- fortify(uscounties, region='id') 
countydf <- join(countypoints, [email protected], by='id') 

ggplot(countydf[countydf$STATEFP=='12' ,])+ 
    aes(long, lat, group=COUNTYNS, fill=STATEFP)+ 
    geom_polygon()+ 
    geom_path(color="black")+ 
    coord_equal() 

下面是所得情節: 佛羅里達僅僅是一個例子。大多數其他州也會在繪圖中顯示類似的錯誤。任何想法如何解決?

+0

當我試圖運行你的代碼,我在'readOGR(。,'cb_2013_us_county_500k')''中得到錯誤''nchar(dsn):object'。'錯誤。找不到'。你能否讓你的代碼可重現? – Andrie 2014-11-22 16:54:32

+0

另外,哪個包是函數'join()'? – Andrie 2014-11-22 17:00:33

回答

2

你爲什麼設置group=COUNTYNS?使用group=id

ggplot(countydf[countydf$STATEFP=='12' ,])+ 
    aes(long, lat, group=group, fill=STATEFP)+ 
    geom_polygon()+geom_path(color="black")+coord_equal() 

您需要發佈提問時多加小心,以免浪費人民的時間.. join(...)是包plyrdplyr

readOGR(.,"...") 

沒有按沒有工作,你需要

readOGR(dsn=".",layer="...") 
+0

在類似的問題中,您的解決方案適用於我,但僅限於在代碼塊中使用'group = group'時。 「group = id」版本仍然會生成工件。 – 2015-06-05 09:14:30