将属性从 csv 文件连接到 dbf 文件后,在 R 中打开 .shp 文件时遇到问题。我在 R 中有很多编码经验,但在 R 中使用 GIS 的经验有限。我在 ArcGIS 方面有经验,但无法再访问该程序。我知道如何使用 csv 文件和绘图点在 R 中创建气泡图和其他地图,但我希望能够将属性添加到 .dbf,然后使用 shapefile 用 brewer 调色板填充县区。在将属性加入 .dbf 文件之前,我可以很好地打开形状文件(这些文件是从美国人口普查局网页获得的)。
下面是我的代码:
library(gpclib)
library(maptools)
library(RColorBrewer)
library(classInt)
library(TeachingDemos)
gci<-read.csv("C:/Users/Smackbug/marketingmapexample.csv", header=TRUE) #Has Geo_ID
#read in dbf file to append data
gci2<-gci
gci2<-na.omit(gci2) #remove any empty data points
#read in dbf file to add attributes
akdbf<-read.dbf(file.choose())#downloaded from the us census bureau
#merge to join attributes
joined<-merge(akdbf,gci2, by=c("GEO_ID"))
#Save original and new dbf
write.dbf(akdbf, "C:/Users/Smackbug/Desktop/shapefiles/gz_2010_02_060_00_500koriginal.dbf")
write.dbf(joined, "C:/Users/Smackbug/Desktop/shapefiles/gz_2010_02_060_00_500k.dbf")
我从这部分代码中得到错误
**alaska<-readShapePoly(file.choose(),proj4string=CRS("+proj=longlat") )
Error in `row.names<-.data.frame`(`*tmp*`, value = value) :
invalid 'row.names' length**
和其余的代码
#the rest of the code should look something like this
colors<-brewer.pal(5,"Reds")
brks<-classIntervals(alaska$medianIncome, n=5, style="fixed", fixedBreaks=c(0,25,50,100,250))
plot(brks, pal=colors)
brks<-brks$brks
plot(alaska, col=colors[findInterval(alaska$medianIncome, brks, all.inside=TRUE)], axes=F)