以下代碼對我很好。自定義一點,你就完成了。 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