以下代码对我很好。自定义一点,你就完成了。 alt text http://files.eduardoleoni.com/map.png
library(maptools)
substitute your shapefiles here
state.map <- readShapeSpatial("BRASIL.shp")
counties.map <- readShapeSpatial("55mu2500gsd.shp")
## this is the variable we will be plotting
[email protected]$noise <- rnorm(nrow([email protected]))
热力图功能
plot.heat <- function(counties.map,state.map,z,title=NULL,breaks=NULL,reverse=FALSE,cex.legend=1,bw=.2,col.vec=NULL,plot.legend=TRUE) {
##Break down the value variable
if (is.null(breaks)) {
breaks=
seq(
floor(min([email protected][,z],na.rm=TRUE)*10)/10
,
ceiling(max([email protected][,z],na.rm=TRUE)*10)/10
,.1)
}
[email protected]$zCat <- cut([email protected][,z],breaks,include.lowest=TRUE)
cutpoints <- levels([email protected]$zCat)
if (is.null(col.vec)) col.vec <- heat.colors(length(levels([email protected]$zCat)))
if (reverse) {
cutpointsColors <- rev(col.vec)
} else {
cutpointsColors <- col.vec
}
levels([email protected]$zCat) <- cutpointsColors
plot(counties.map,border=gray(.8), lwd=bw,axes = FALSE, las = 1,col=as.character([email protected]$zCat))
if (!is.null(state.map)) {
plot(state.map,add=TRUE,lwd=1)
}
##with(counties.map.c,text(x,y,name,cex=0.75))
if (plot.legend) legend("bottomleft", cutpoints, fill = cutpointsColors,bty="n",title=title,cex=cex.legend)
##title("Cartogram")
}
情节它
plot.heat(counties.map,state.map,z="noise",breaks=c(-Inf,-2,-1,0,1,2,Inf))
请注意,此类型的映射被称为等值线,并且有一些非常重大的问题,即较小的地理区域往往有更多的人(如东海岸对蒙大拿州),所以视觉外观偏向于人口密度低的地区。 – hadley 2009-08-12 01:50:19
另外,如果你正在处理原始的ESRI数据,你可能会发现它有太多的顶点。在R中泛化的粗略尝试可以在http://github.com/hadley/data-counties/tree/master – hadley 2009-08-12 01:55:07
@hadley找到,我完全赞同你对“问题”的看法。这通常是数据空间表示的一个问题。 – 2009-08-12 15:50:14