中国省级地图绘制

参考: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

你可能感兴趣的:(中国省级地图绘制)