三、R语言可视化--ggplot2和REmap包绘制地图

xiaoxiao2021-02-28  91

绘制地图

1.ggplot2提供了一些工具,让使用maps包绘制的地图与其他ggplot2图形的结合变得十分方便。关于中国地图的绘制可以参考:http://cos.name/2009/07/drawing-china-map-using-r/

国家maps地图数据名

法国

意大利

新西兰

美国(郡)

美国(州)

美国(边界)

全世界

france

italy

nz

country

state

usa

world

 

我们使用地图数据主要有两种原因:1.为空间数据添加参考轮廓线2.通过不同的区域填充颜色以构建等值线图。

添加地图边界可以通过borders()来完成。函数的前两个参数指定了要绘制的地图名map以及其中的具体区域region,其余的参数用于控制边界的外观。填充颜色的多边形使用的是fill。

1 2 3 4 >  library (maps) >  data (us.cities) > big_citi <-  subset (us.cities,pop > 500000)   ##人口大于50万的城市 >  qplot (long,lat,data=big_citi) +  borders ( "state" ,size=0.5)

  

1 2 > tx_city <-  subset (us.cities,country.etc== "TX" ) >  ggplot (tx_city, aes (long,lat)) +  borders ( "county" , "texas" ,colour= "grey70" ) +  geom_point (colour =  "black" ,alpha=0.5)

  德克萨斯州的城市区划:

使用map_data将地图数据转化为数据框,此数据框可以在之后通过merge() 操作与我们的数据相融合,最终绘制处等值线图。

1 2 3 4 5 6 7 8 9 > states <-  map_data ( "state" ) > arrests <- USArrests >  names (arrests) <-  tolower ( names (arrests)) > arrests$region <-  tolower ( rownames (USArrests))   > choro <-  merge (states,arrests,by= "region" ) #由于绘制多边形时涉及顺序问题,且merge破坏了原始排序,故将行重新排序 > choro <- choro[ order (choro$order),] >  qplot (long,lat,data=choro,group=group,fill=assault,geom= "polygon" )

  

#######################华丽的分割线############################

接下来简单介绍一下中国地图的绘制。

首先要成功安装好两个包:

1 2 >  install.packages ( "maps" ) >  install.packages ( "mapdata" )

  然后加载:

1 2 >  library (maps) >  library (mapdata)

  加载成功后我们可以使用map方法绘制中国地图:

1 >  map ( "china" )

  

以上的地图比较老了(重庆仍然在四川省内),获取比较新的,下载中国GIS数据(http://cos.name/wp-content/uploads/2009/07/chinaprovinceborderdata_tar_gz.zip),解压到我的文档中(默认的R工作空间),然后下载maptools包:install.packages("maptools"),加载到R的工作空间:library(maptools),然后绘制地图:

1 2 > x <-  readShapePoly ( 'bou2_4p.shp' ) >  plot (x)

  

通过调节plot命令中的col(旧版为fg)参数来根据自己的需要对地图中的省份着以特定的颜色。

GIS数据包含925个多边形,col参数应该时一个长度为925的向量,第i个分量的取值就代表了地图中第i个多边形的颜色:

1 >  plot (x,col= gray (924:0/924))

  

在地图中只画出部分省份所代表的区域:

1 2 3 4 5 6 7 8 9 10 11 x= readShapePoly ( 'bou2_4p.shp' ); getColor= function (mapdata,provname,provcol,othercol) {      f= function (x,y)  ifelse (x % in % y, which (y==x),0);      colIndex= sapply (mapdata@data$NAME,f,provname);      fg= c (othercol,provcol)[colIndex+1];       return (fg); } midchina <-  c ( "河南省" , "山西省" , "湖北省" , "安徽省" , "湖南省" , "浙江省" ); plot (x, col =  getColor (x, midchina,  rep ( "green" , 6),      "white" ), border =  "black" , xlab =  "" , ylab =  "" )

  ##若不显示其他省份,只需将border设置为"white"即可。

  

根据人口的多少,对区域进行着色:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 provname= c ( "北京市" , "天津市" , "河北省" , "山西省" , "内蒙古自治区" ,          "辽宁省" , "吉林省" , "黑龙江省" , "上海市" , "江苏省" ,          "浙江省" , "安徽省" , "福建省" , "江西省" , "山东省" ,          "河南省" , "湖北省" , "湖南省" , "广东省" ,          "广西壮族自治区" , "海南省" , "重庆市" , "四川省" , "贵州省" ,          "云南省" , "西藏自治区" , "陕西省" , "甘肃省" , "青海省" ,          "宁夏回族自治区" , "新疆维吾尔自治区" , "台湾省" ,          "香港特别行政区" ); pop= c (1633,1115,6943,3393,2405,4298,2730,3824,1858,7625,          5060,6118,3581,4368,9367,9360,5699,6355,9449,          4768,845,2816,8127,3762,4514,284,3748,2617,          552,610,2095,2296,693); provcol= rgb (red=1-pop/ max (pop)/2,green=1-pop/ max (pop)/2,blue=0); plot (x,col= getColor (x,provname,provcol, "white" ),xlab= "" ,ylab= "" );

  

颜色越深的地方表示人口越密集。

 2.还有ERmap包,它是可交互的地图数据可视化工具,托管在github,基于echarts开发的一个包,它本身提供的参数也比较少。

2.1迁移图

1 2 3 4 5 > cong1 <-  c ( "bei jing" , "chang sha" , "shang hai" , "hang zhou" ) > dao2 <-  c ( "chang sha" , "shang hai" , "hang zhou" , "xia men" ) > dat2 <-  data.frame (cong1,dao2) > out <-  remap (dat2) >  plot (out)

  

这个包有个严重的问题是,你输入中文的时候,它有时候无法辨别,幸好当你输入地名的时候可以使用拼音,也不区分大小写,比如输入西安的时候,你可以输入“xi an”也可以“xi an shi”.

2.2颜色等级图

1 2 3 4 > city <-  c ( "上海" , "浙江" , "四川" , "北京" ) > value <-  c (3734,3248,2361,3305) > cdata <-  data.frame (city,value) >  remapC (cdata,maptype= "China" ,color= 'skyblue' )

  

上图因为鼠标移动到浙江区域上,所以高亮黄色显示。同时标签信息在途中显示。

如果我们想在颜色等级图上添加迁移图特点,那我们就要用到marklineData和markPointData这两个参数,它们默认为NULL。

我们下面添加气泡:

1 >  remapC (cdata,maptype =  "China" ,color =  'skyblue' ,markPointData=dat)

 

添加箭头:

1 >  remapC (cdata,maptype= "China" ,color= 'skyblue' ,markLineData=dat)

  

如果即要有箭头,又要又气泡:

1 >  remapC (cdata,maptype= "China" ,color= 'skyblue' ,markLineData=dat,markPointData=dat)

  

2.3 热力图

现在我们来继续绘制热力图。数据是随便取的。

1 2 3 4 5 6 7 8 9 10 11 > city <-  c ( "xia men" , "shang hai" , "bei jing" , "chong qing" ) > heat <-  c (0.9,0.6,0.8,0.7) > temp <-  get_geo_position ( as.vector (city))   ##获取坐标 > heatdata <-  data.frame (temp$lon,temp$lat,heat) > heatdata    temp.lon temp.lat heat 1 118.0959 24.48582  0.9 2 121.4802 31.23631  0.6 3 116.4136 39.91101  0.8 4 106.5572 29.57100  0.7 >  remapH (heatdata,minAlpha=0.51,title= "Heat Map from REmap" )

  

 

 

好文要顶  关注我  收藏该文 
转载请注明原文地址: https://www.6miu.com/read-2627330.html

最新回复(0)