2
首先,我爲標題道歉,但我不完全確定如何描述我的地圖(因此包含圖像)正在發生的事情。我是用ggplot2進行繪圖的新手,對於我的第一個「out of tutorial」地圖,我試圖通過DC中的道路顯示人口普查數據。通過教程搜索似乎支持我已採取的程序,但顯然我正在偏離課程(並且我還沒有看到這個的另一個實例)。人口普查數據w/ggplot2 - Haphazard poly構造(現在w /地圖!)
我們的目標是合併來自.csv的數據,我已經準備好了包含人口普查區域多邊形的shapefile文件。起初,我認爲這與行的排序有關,但明確說明這一點似乎沒有改變任何東西。
代碼:
###Combining Census data with a tract poly shapefile
library(maptools)
library(ggplot2)
library(gpclib)
setwd('~/ESRI/Trend Report Maps')
#Read data
tract<-readShapePoly('TractPly.shp')
cdata<-read.csv('census00_10.csv')
#Columns >> note that GEOID (tract) and geoid2 (cdata) are the merge keys
ntract<-names(tract)
ncdata<-names(cdata)
#Prepare data for ggplot plotting of poly info (by conversion to DataFrame)
gpclibPermit()
tract_geom<-fortify(tract,region="GEOID")
#Note that this drops attribute and retains only spatial info. However, we don't really
#need the attribute info since we are joining it to the other dataframe
#Merge
tract_poly<-merge(tract_geom,cdata,by.x="id",by.y="geoid2")
tract_poly<-tract_poly[order(tract_poly$order),]
head(tract_poly)
workF<-ggplot(tract_poly,aes(long,lat,group-group,fill=dmed_age)) +
geom_polygon() +
coord_equal() #fixes aspect ratio
workF
呀,人口普查數據的可視化已經看到更好的日子。如果我不被允許顯示地圖,它看起來像一個雜亂無章的線條(不像在有人將紙張摺疊多次並在展開之前隨機切割紙張時的樣子)。儘管華盛頓特區的外圍仍然保持完整,但所有形狀都不像人口普查區。我可以確認shapefile是從ArcMap導出的(它看起來應該是這樣)。
會議信息:
> sessionInfo()
R version 2.15.3 (2013-03-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] gpclib_1.5-1 ggplot2_0.9.3.1 maptools_0.8-23 lattice_0.20-13 sp_1.0-5 foreign_0.8-52
loaded via a namespace (and not attached):
[1] colorspace_1.2-1 dichromat_2.0-0 digest_0.6.3 gtable_0.1.2 labeling_0.1
[6] MASS_7.3-23 munsell_0.4 plyr_1.8 proto_0.3-10 RColorBrewer_1.0-5
[11] reshape2_1.2.2 scales_0.2.3 stringr_0.6.2 tools_2.15.3
我一定看過這段代碼,跳過了100遍。謝謝,就是這樣。 –