[英]Developing Geographic Thematic Maps with R
R中有很多包用於各種空間分析。 這可以在CRAN任務視圖中看到:空間數據分析 。 這些軟件包數量眾多且各種各樣,但我想做的只是一些簡單的專題圖 。 我有縣和州FIPS代碼的數據,我有縣和州邊界的ESRI形狀文件和隨附的FIPS代碼,允許加入數據。 如果需要,形狀文件可以很容易地轉換為其他格式。
那么用R創建專題地圖最直接的方法是什么?
這張地圖看起來像是用ESRI Arc產品創建的,但這是我想用R做的事情:
alt text http://www.infousagov.com/images/choro.jpg 從這里復制的地圖。
以下代碼對我很有幫助。 稍微定制它就完成了。
(來源: eduardoleoni.com )
library(maptools)
substitute your shapefiles here
state.map <- readShapeSpatial("BRASIL.shp")
counties.map <- readShapeSpatial("55mu2500gsd.shp")
## this is the variable we will be plotting
counties.map@data$noise <- rnorm(nrow(counties.map@data))
熱圖功能
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(counties.map@data[,z],na.rm=TRUE)*10)/10
,
ceiling(max(counties.map@data[,z],na.rm=TRUE)*10)/10
,.1)
}
counties.map@data$zCat <- cut(counties.map@data[,z],breaks,include.lowest=TRUE)
cutpoints <- levels(counties.map@data$zCat)
if (is.null(col.vec)) col.vec <- heat.colors(length(levels(counties.map@data$zCat)))
if (reverse) {
cutpointsColors <- rev(col.vec)
} else {
cutpointsColors <- col.vec
}
levels(counties.map@data$zCat) <- cutpointsColors
plot(counties.map,border=gray(.8), lwd=bw,axes = FALSE, las = 1,col=as.character(counties.map@data$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))
以為我會在這里添加一些新信息,因為自發布以來,圍繞此主題已經有了一些活動。 以下是Revolutions博客上“Choropleth Map R Challenge”的兩個很棒的鏈接:
希望這些對於查看此問題的人有用。
祝一切順利,
松鴉
看看包裹
library(sp)
library(rgdal)
這對地理數據很好,而且
library(RColorBrewer)
對着色很有用。 這張地圖是用上面的包和這段代碼制作的:
VegMap <- readOGR(".", "VegMapFile")
Veg9<-brewer.pal(9,'Set2')
spplot(VegMap, "Veg", col.regions=Veg9,
+at=c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5),
+main='Vegetation map')
"VegMapFile"
是一個shapefile, "Veg"
是顯示的變量。 一點點工作可能會做得更好。 我似乎不允許上傳圖片,這里是圖片的鏈接:
看一下PBSmapping包(請參閱插圖/手冊和演示)以及這篇 O'Reilly Data Mashups的R文章(不幸的是它不是免費的,但根據Revolutions博客下載價值4.99美元)。
這只是三行!
library(maps);
colors = floor(runif(63)*657);
map("state", col = colors, fill = T, resolution = 0)
完成! 只需將第二行更改為63個元素的任何向量(0到657之間的每個元素,它們是colors()的成員)
現在,如果你想得到幻想,你可以寫:
library(maps);
library(mapproj);
colors = floor(runif(63)*657);
map("state", col = colors, fill = T, projection = "polyconic", resolution = 0);
63個元素代表63個區域,您可以通過運行獲得其名稱:
map("state")$names;
R Graphics Gallery有一個非常相似的地圖 ,應該是一個很好的起點。 代碼在這里:www.ai.rug.nl/~hedderik/R/US2004。 您需要使用legend()函數添加圖例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.