2

首先,我为标题道歉,但我不完全确定如何描述我的地图发生的事情(因此包含图像)。我是使用 ggplot2 映射的新手,对于我的第一个“教程外”地图,我试图在 DC 中按区域显示人口普查数据。搜索教程似乎支持我所采取的程序,但显然我正在偏离路线(而且我还没有看到另一个例子)。

目标是将我准备的 .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      
4

1 回答 1

1

这只是一个错字。更改group-groupgroup=group,你应该没问题。

于 2013-04-26T12:47:05.010 回答