中国省级地图绘制

参考:https://www.cnblogs.com/skyme/p/5182149.html

两种方法绘制中国地图:

  • 使用ggplot2绘制地图
    这个方法比较规范,可以标注各个省的特点(可以是名称)和标注省会城市。
  • 直接使用plot绘制下载的GIS数据
    这个方法比较灵活,但是规范性比较差。

数据准备:

  1. 下载中国地图的GIS数据
wget -cO chinaprovinceborderdata_tar_gz.zip "https://uploads.cosx.org/2009/07/chinaprovinceborderdata_tar_gz.zip"
  1. 解压到能够找到的位置
    比如:~/Install/chinaGisMap/bou2_4p.*

方法1:ggplot方式画图
ggplot绘制地图可以使用map函数。也可以使用GIS数据绘制。

library(maptools)
library(ggplot2)
library(maps)
library(mapdata)

mapShape_read = function(shp = ""){
  shpDat = maptools::readShapePoly(shp)
  provName = shpDat@data$NAME
  list(mapData = shpDat, provName = provName)
}

vctSupply = function(vct1 = c(), vct2 = c()){
  if(length(vct1) != length(vct2)){
    stop("Error: vct1 != vct2")
  }
  loci = which(!vct2 %in% NA)
  vct1[loci] = vct2[loci]
  vct1
}

featureName_get = function(map_shp = NULL, provName = c(), provFeat = c(), otherFeat = "other"){
  tt_feature <- rep(otherFeat, length(map_shp$provName))
  new_feature <- rep(NA, length(map_shp$provName))
  loci = match(map_shp$provName %>% iconv("GBK","utf8"),provName)
  tt_feature = vctSupply(tt_feature, provFeat[loci])
  tt_feature
}

# provName:省名称
# provFeat:省特点
chinaMap_plot = function(provName = c(), provFeat = c()){
  
  map_shp = mapShape_read(file.choose()) # 选择解压的*.shp文件
  tt_provName = map_shp$provName %>% iconv(from= "gbk",to="UTF8")
  
  id_fetch = function(xx){
    xx@ID
  }
  idCodList = lapply(map_shp$mapData@polygons, id_fetch)
  id_cods = do.call("c",idCodList)
  
  featureName = featureName_get(map_shp = map_shp, provName = provName, provFeat = provFeat);  
  
  rgInfo = data.frame(id = id_cods, provName = tt_provName, featureName = featureName)
  
  xx_fetch = function(xx){
    xx@Polygons[[1]]@coords[,1]
  }
  # xx = map_data@polygons[[1]]
  x_cod = lapply(map_shp$mapData@polygons, xx_fetch)
  yy_fetch = function(xx){
    xx@Polygons[[1]]@coords[,2]
  }
  y_cod = lapply(map_shp$mapData@polygons, yy_fetch)
  
  id_fetch = function(xx){
    xx@ID
  }
  
  regionPositionPre = Map(function(xx,yy,zz){
    data.frame(id = xx, x = yy, y = zz)
  }, idCodList, x_cod, y_cod)
  regionPosition = do.call(rbind, regionPositionPre)
  
  beijing = c('北京&天津', 39.90419989999999, 116.4073963, 1961.24 + 1293.82) 
  shanghai = c('上海', 31.2303904, 121.4737021, 2301.91) 
  zhengzhou = c('郑州', 34.7472541716, 113.6249284647, 862.65) 
  wulumuqi = c('乌鲁木齐', 43.8266013700, 87.6168405804, 311.03) 
  haerbin = c('哈尔滨', 45.8021755616, 126.5358247345, 1063.6) 
  xian = c('西安', 34.3412614674, 108.9398165260, 846.78) 
  wuhan = c('武汉', 30.5927599029, 114.3052387810, 978.54) 
  chengdu = c('成都', 30.5702183724, 104.0647735044, 1404.76) 
  lasa = c('拉萨', 29.6441135160, 91.1144530801, 55.94) 
  chongqing = c('重庆', 29.5647048135, 106.5507137149, 2884.62) 
  kunming = c('昆明', 24.8796595146, 102.8332118852, 643.2) 
  guangshen = c('广州&深圳', 23.0206747828, 113.7517837567, 1270.08 + 1035.79) 
  cities = c(beijing, shanghai, zhengzhou, wulumuqi, haerbin, xian, wuhan, chengdu, lasa, chongqing, kunming, guangshen) 
  mat.cities = as.data.frame(matrix(cities, ncol = 4, byrow = T), stringsAsFactors = F) 
  names(mat.cities) = c('names', 'lat', 'long', 'population') 
  mat.cities$population = as.numeric(as.character(mat.cities$population)) / 100 
  # mat.cities$population = NA
  mat.cities$lat = as.numeric(as.character(mat.cities$lat)) 
  mat.cities$long = as.numeric(as.character(mat.cities$long))
  
  
  plt = ggplot(data = rgInfo) + 
    geom_map(
      mapping = aes(map_id = id, fill=featureName, colour="border"),
      map = regionPosition
    ) +
    expand_limits(regionPosition) 
  
  plt = plt + geom_point(
    data = mat.cities,
    mapping = aes(x = long, y = lat), 
    alpha = 0.8, 
    color = '#8BB6D6') + 
    geom_text(
      data = mat.cities,
      mapping = aes(x = long,y = lat,label = names)
    )    
  plt
}

provName = c("四川省", "云南省", "浙江省","福建省","江苏省")
provFeat = c("~四川省", "~云南省","~浙江省","~福建省","~江苏省")
chinaMap_plot(provName = provName , provFeat = provFeat)

效果图:


plot_map_png.png

方法2:使用plot方式画中国地图

mapShape_read = function(shp = ""){
  shpDat = maptools::readShapePoly(shp)
  provName = shpDat@data$NAME
  list(mapData = shpDat, provName = provName)
}
vctSupply = function(vct1 = c(), vct2 = c()){
  if(length(vct1) != length(vct2)){
    stop("Error: vct1 != vct2")
  }
  loci = which(!vct2 %in% NA)
  vct1[loci] = vct2[loci]
  vct1
}
col_get = function(map_shp = NULL, provName = c(), provCol = c(), otherCol = "white"){
  cols <- rep("white", length(map_shp$provName))
  mch = match(str_conv(map_shp$provName, "GBK"), provName)
  cols = vctSupply(cols, provCol[mch])
  cols
}

map_shp = mapShape_read(file.choose()) # 选择解压的*.shp文件

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);

xy_fetch = function(xx){
  data.frame(x = xx@Polygons[[1]]@coords[,1], 
             y = xx@Polygons[[1]]@coords[,2])
}
# xx = map_shp$mapData@polygons[[1]]
xy_cod = lapply(map_shp$mapData@polygons, xy_fetch)
xy_df = do.call(rbind, xy_cod)

cols = col_get(map_shp, provName = provname, provCol = provcol);
plot(map_shp$mapData,col=cols,xlab="",ylab="");

beijing = c('北京&天津', 39.90419989999999, 116.4073963, 1961.24 + 1293.82) 
shanghai = c('上海', 31.2303904, 121.4737021, 2301.91) 
zhengzhou = c('郑州', 34.7472541716, 113.6249284647, 862.65) 
wulumuqi = c('乌鲁木齐', 43.8266013700, 87.6168405804, 311.03) 
haerbin = c('哈尔滨', 45.8021755616, 126.5358247345, 1063.6) 
xian = c('西安', 34.3412614674, 108.9398165260, 846.78) 
wuhan = c('武汉', 30.5927599029, 114.3052387810, 978.54) 
chengdu = c('成都', 30.5702183724, 104.0647735044, 1404.76) 
lasa = c('拉萨', 29.6441135160, 91.1144530801, 55.94) 
chongqing = c('重庆', 29.5647048135, 106.5507137149, 2884.62) 
kunming = c('昆明', 24.8796595146, 102.8332118852, 643.2) 
guangshen = c('广州&深圳', 23.0206747828, 113.7517837567, 1270.08 + 1035.79) 
cities = c(beijing, shanghai, zhengzhou, wulumuqi, haerbin, xian, wuhan, chengdu, lasa, chongqing, kunming, guangshen) 
mat.cities = as.data.frame(matrix(cities, ncol = 4, byrow = T), stringsAsFactors = F) 
names(mat.cities) = c('names', 'lat', 'long', 'population') 
mat.cities$population = as.numeric(as.character(mat.cities$population)) / 100 
# mat.cities$population = NA
mat.cities$lat = as.numeric(as.character(mat.cities$lat)) 
mat.cities$long = as.numeric(as.character(mat.cities$long))

points(mat.cities$long, mat.cities$lat, type= "p", pch=20)
text(mat.cities$long, mat.cities$lat, labels = mat.cities$names, adj = c(1,1))

效果图:


plot_map_png.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,377评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,390评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,967评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,344评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,441评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,492评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,497评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,274评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,732评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,008评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,184评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,837评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,520评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,156评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,407评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,056评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,074评论 2 352

推荐阅读更多精彩内容