R语言在矢量地图上绘制分级设色散点图

实现效果:

R语言中ggplot2包提供绘制地图、散点图的方法,是实现在矢量地图上绘制分级设色散点图核心包

  • 绘制多边形 geom_polygon(data,aes,fill, colour)
  • 绘制点 geom_point
  • .....

0、需要用到的包

library(maptools)  # 读取shp数据常用,可以将shp数据读取为SpatialPolygonsDataFrame  格式,为DataFrame(数据帧)子类,也称为空间多边形数据帧
library(ggplot2)  #绘图核心类
library(plyr)  #join方法 合并两个单元格用(空间数据+属性数据),实现相当于ArcGIS中的空间连接

1、读取底图shp数据

所需数据:图层格式为多边形的shp文件即可。

底图可以使用shp数据直接读入,利用maptools包

#导入地理信息数据:
china_map <- readShapePoly("geodata/TP_WGS84.shp")            # 读取地图信息数据
china_map1 <- fortify(china_map)                               #转化为数据框

 china_map使用maptools包中的读取shp多边形方法读入, china_map格式为SpatialPolygonsDataFrame 

由于ggplot2绘制地图需要用到 特定的数据框格式,使用ggplot2提供的fortify(方法),将其SpatialPolygonsDataFrame 转化为如下格式

      long      lat order  hole piece id group
1 87.15341 27.83085     1 FALSE     1  0   0.1
2 87.08135 27.89600     2 FALSE     1  0   0.1
3 87.05430 27.93455     3 FALSE     1  0   0.1
4 87.00324 27.94043     4 FALSE     1  0   0.1
5 86.93428 27.95976     5 FALSE     1  0   0.1
6 86.88593 28.02092     6 FALSE     1  0   0.1

 2、读取散点图信息数据

所需数据:保存为csv格式的 带坐标的数据,e.g.

即使用简单读入csv的方法

mydata <- read.csv("data/T_rescpannual.csv")             #读取业务数据

3、使用ggplot2可视化渲染 

ggplot()+
  geom_polygon(data=china_map1, aes(x=long, y=lat, group=group), fill="grey95", colour="grey60")+ 
  geom_point(data=mydata, aes(x = long,y = lat, size=4, fill=X1983, alpha=0.3), shape=21, colour="black")+ 
#  scale_size_area(max_size=4)+         
  scale_fill_gradient2(low="DarkCyan", mid="Azure", high="Sienna", midpoint=0)+         
  coord_map("polyconic") +ggtitle("Annual temperature of meteorological stations in Tibet Plateau")+
  theme(
    panel.grid = element_blank(),
    panel.background = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank(),
    legend.position = "bottom"
  )  

全部代码:

library(maptools)
library(ggplot2)
library(plyr)

#导入地理信息数据:
china_map <- readShapePoly("geodata/TP_WGS84.shp")            # 读取地图信息数据
china_map1 <- fortify(china_map)                               #转化为数据框

#业务数据导入及作图数据合并:
mydata <- read.csv("data/T_rescpannual.csv")             #读取业务数据
#province_city <- read.csv("c:/rstudy/chinaprovincecity.csv")   #读取省会城市坐标
china_data <- join(province_city, mydata, type="full")        #合并两个数据框

#地图语句:
ggplot()+
  geom_polygon(data=china_map1, aes(x=long, y=lat, group=group), fill="grey95", colour="grey60")+ 
  geom_point(data=mydata, aes(x = long,y = lat, size=4, fill=X1983, alpha=0.3), shape=21, colour="black")+ 
#  scale_size_area(max_size=4)+         
  scale_fill_gradient2(low="DarkCyan", mid="Azure", high="Sienna", midpoint=0)+         
  coord_map("polyconic") +ggtitle("Annual temperature of meteorological stations in Tibet Plateau")+
  theme(
    panel.grid = element_blank(),
    panel.background = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank(),
    legend.position = "bottom"
  )  

 参考文献:

R语言可视化——地图与气泡图结合应用

https://www.cnblogs.com/nxld/p/6059603.html

 

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问,以下是一个基于leaflet的分级地图代码实例: ```javascript // 创建地图对象 var map = L.map('map').setView([51.505, -0.09], 13); // 添加地图瓦片层 L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>', maxZoom: 18, }).addTo(map); // 定义色阶数组 var colors = ['#ffffcc', '#c2e699', '#78c679', '#31a354', '#006837']; // 定义分级色函数 function getColor(d) { return d > 1000 ? colors[4] : d > 500 ? colors[3] : d > 200 ? colors[2] : d > 100 ? colors[1] : colors[0]; } // 定义GeoJSON数据 var geojson = { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [-0.09, 51.505] }, "properties": { "name": "London", "population": 8673719 } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [-73.935242, 40.730610] }, "properties": { "name": "New York", "population": 8622698 } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [139.691706, 35.689487] }, "properties": { "name": "Tokyo", "population": 13929286 } } ] }; // 添加GeoJSON层 L.geoJSON(geojson, { pointToLayer: function (feature, latlng) { return L.circleMarker(latlng, { radius: Math.sqrt(feature.properties.population) / 100, fillColor: getColor(feature.properties.population), color: "#000", weight: 1, opacity: 1, fillOpacity: 0.8 }).bindPopup(feature.properties.name + "<br>Population: " + feature.properties.population); } }).addTo(map); ``` 希望这个代码实例对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值