使用R绘制热力地图

使用R绘制热力地图

题目:利用WHO数据集,描画亚洲国家的热力地图,标识Adult literacy rate(成人识字率)项指标的直观展现。注:WHO数据集是WHO(World Health Organization)收集的世界各国健康方面的数据集。

一、导入数据 && 数据预处理

> who<-read.csv("D:\\Desktop\\lianshu\\R06\\WHO.csv")
> dim(who)
[1] 202 358

可以看出该数据集包含了世界上202个国家的人民健康数据。
找出"Adult.literacy.rate"数据所在的列:

> which(names(who)=="Adult.literacy.rate")
[1] 5

取子集:

> who<-who[,c(1,2,3,which(names(who)=="Adult.literacy.rate"))]
> head(who)
              Country CountryID Continent Adult.literacy.rate
1         Afghanistan         1         1                28.0
2             Albania         2         2                98.7
3             Algeria         3         3                69.9
4             Andorra         4         2                  NA
5              Angola         5         3                67.4
6 Antigua and Barbuda         6         4                  NA

亚洲的Continent为7,取出只包含亚洲国家的数据集:

> who<-who[who$Continent==7,]
> who
       Country CountryID Continent Adult.literacy.rate
14  Bangladesh        14         7                47.5
30    Cambodia        30         7                73.6
37       China        37         7                90.9
80       India        80         7                61.0
110   Maldives       110         7                96.3
127      Nepal       127         7                48.6
138   Pakistan       138         7                49.9
150     Russia       150         7                99.4
170  Sri Lanka       170         7                90.7

可以看到, Bhutan(不丹这个国家的Adult.literacy.rate值为NA)。所以,过滤掉缺失的数据

> who<-na.omit(who)

二、绘制热力图

需要用到maps包。 把Adult.literacy.rate的值赋给每个国家。

> x<-map("world",mar=c(0,0,0,0),regions = who$Country,plot = FALSE)
> for(i in 1:nrow(who))
+   for(j in 1:length(x$names)) {
+     if(grepl(who$Country[i],x$names[j],ignore.case=T)){
+       x$measure[j]<-as.double(who$Adult.literacy.rate[i])
+     }
+   }


#设置热力图的颜色

> library(RColorBrewer)
> colors <-brewer.pal(7,"Reds")
> r1<-range(who$Adult.literacy.rate,na.rm = TRUE)
> breaks<-seq(r1[1],r1[2]*1.001,length.out = 8)

#设置页面布局为一行两列。

> ss<-layout(matrix(data=c(2,1), nrow=1, ncol=2),widths=c(8,1), heights=c(8,1))
#绘制右侧热力图例
> par(mar = c(10,1,10,4),oma=c(0.2,0.2,0.2,0.2),mex=0.5)
> image(x=1, y=0:length(breaks),z=t(matrix(breaks))*1.001,
+       col=colors[1:length(breaks)-1],axes=FALSE,breaks=breaks,
+       xlab="", ylab="", xaxt="n")
> axis(4,at=0:(length(breaks)-1),
+      labels=round(breaks),col="white",las=1)
> abline(h=c(1:length(breaks)),col="white",lwd=2,xpd=F)
绘制热力地图
> par(mar = c(5,2,10,5),oma=c(0.2,0.2,0.2,0.2),mex=0.5)
> image(x=1, y=0:length(breaks),z=t(matrix(breaks))*1.001,
+       col=colors[1:length(breaks)-1],axes=FALSE,breaks=breaks,
+       xlab="", ylab="", xaxt="n")
> axis(4,at=0:(length(breaks)-1),
+      labels=round(breaks),col="white",las=1)
> abline(h=c(1:length(breaks)),col="white",lwd=2,xpd=F)
> 
> #Map
> map("world",regions = who$Country,col =colors[findInterval(x$measure,breaks)],fill=TRUE,lty="blank" )
> map("world",regions = "Bhutan",add = TRUE)
> title("亚洲国家的Adult literacy rate\n(成人识字率)热力地图")

热力图可以直观的展现各国的成人识字率水平,俄罗斯的成人识字率最高,中国次之。

发布于 2017-05-10