图层状态在地图上排列

时间:2014-08-06 09:41:27

标签: r map plot shapefile

我使用shapefile根据特定条件在R中创建地图文件。代码如下所示

   library('sp')
library('rgdal')
library('maptools')
gpclibPermit()
zip<-readShapePoly("C:/Users/Ishan.Basu/Desktop/shapefile/shapefile ZIP/USZ102.shp")
plot(zip)
ms2<-read.table("C:/Users/Ishan.Basu/Desktop/MS.txt",header=T,sep="\t",colClasses="character",as.is=T)

zip@data = merge(zip@data,ms2, by.x="ZIP", by.y="ZIP")

x<-zip@data
plot(zip,col=ifelse(zip@data$Seg=="Top Performers",'Dark Green',
                      ifelse(zip@data$Seg=="Average Performers",'Light Green',
                    ifelse(zip@data$Seg=="Poor Performers",'Yellow',
                           ifelse(zip@data$Seg=="Zero Returns",'Red',
                                  ifelse(zip@data$Seg=="Zero Activity",'Dark Red',
                                         'Grey'))))),
     border=ifelse(zip@data$Seg=="Top Performers",'Dark Green',
                   ifelse(zip@data$Seg=="Average Performers",'Light Green',
                          ifelse(zip@data$Seg=="Poor Performers",'Yellow',
                                 ifelse(zip@data$Seg=="Zero Returns",'Red',
                                        ifelse(zip@data$Seg=="Zero Activity",'Dark Red',
                                               'Grey'))))),
     bty="n")

现在我想要一种方法,可以在形状文件的顶部绘制状态轮廓。 非常感谢快速回复

1 个答案:

答案 0 :(得分:0)

这对我有用:

png(filename="D:\\analysis_f.png", width=2000, height=2000)
plot(zip,col=ifelse(zip@data$Seg=="Super Top",'Dark Green',
                      ifelse(zip@data$Seg=="Top Performer",'Light Green',
                    ifelse(zip@data$Seg=="Average Performer",'Yellow',
                           ifelse(zip@data$Seg=="Poor Performer",'Red',
                                  ifelse(zip@data$Seg=="Zero returns",'Dark Red',
                                         'Grey'))))),
     border=ifelse(zip@data$Seg=="Super Top",'Dark Green',
                   ifelse(zip@data$Seg=="Top Performer",'Light Green',
                          ifelse(zip@data$Seg=="Average Performer",'Yellow',
                                 ifelse(zip@data$Seg=="Poor Performer",'Red',
                                        ifelse(zip@data$Seg=="Zero returns",'Dark Red',
                                               'Grey'))))),
     bty="n")

map('state', plot = TRUE, fill = FALSE,add=TRUE)
dev.off()