R语言如何在绘制的地图上叠加插入指定属性点位
R空间绘图:在行政区划底图上叠加属性点位
现有基础准备
目前已完成两部分基础内容:
- 整理完成的带经纬度信息的属性点位数据集:
Points_properties = structure (list(Properties=c(1,2,3,4,5,6), Latitude = c(-24.930473, -24.95575,-24.924161,-24.95579, -24.94557, -24.93267), Longitude = c(-49.994889, -49.990162,-50.004343, -50.007371, -50.01542, -50.00702)), row.names=c(NA,6L), class="data.frame") # 数据集预览 > Points_properties Properties Latitude Longitude 1 1 -24.93047 -49.99489 2 2 -24.95575 -49.99016 3 3 -24.92416 -50.00434 4 4 -24.95579 -50.00737 5 5 -24.94557 -50.01542 6 6 -24.93267 -50.00702
- 可正常运行的行政区划底图绘制代码:
library(rgdal) temp <- tempfile() temp2 <- tempfile() download.file("https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/UFs/PR/pr_municipios.zip",temp) unzip(zipfile = temp, exdir = temp2) shp <- readOGR(temp2) plot(shp)
运行上述代码输出的底图效果:
点位叠加实现方案
直接在原有底图代码基础上补充点位转换、绘制逻辑即可,核心是统一点位和底图的坐标参考系,避免点位出现位置偏移,完整可运行代码如下:
library(rgdal) # 读取并绘制底图 temp <- tempfile() temp2 <- tempfile() download.file("https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/UFs/PR/pr_municipios.zip",temp) unzip(zipfile = temp, exdir = temp2) shp <- readOGR(temp2) plot(shp) # 加载点位数据,转换为空间格式 Points_properties = structure (list(Properties=c(1,2,3,4,5,6), Latitude = c(-24.930473, -24.95575,-24.924161,-24.95579, -24.94557, -24.93267), Longitude = c(-49.994889, -49.990162,-50.004343, -50.007371, -50.01542, -50.00702)), row.names=c(NA,6L), class="data.frame") # 指定经纬度坐标列,初始坐标系设为通用WGS84(EPSG:4326) coordinates(Points_properties) <- ~Longitude + Latitude proj4string(Points_properties) <- CRS("+init=epsg:4326") # 将点位投影转换为和底图完全一致的坐标系,杜绝位置偏移 points_aligned <- spTransform(Points_properties, proj4string(shp)) # 叠加绘制点位 points(points_aligned, col = "red", pch = 19, cex = 1.2) # 可选:添加点位对应的属性编号标签 text(points_aligned, labels = points_aligned$Properties, pos = 3, cex = 0.9, col = "darkred")
参数调整说明
- 点位样式可按需修改:
col控制点颜色,pch控制点形状,cex控制点大小 - 如果不需要显示属性编号,直接删除最后一行
text()相关代码即可 - 坐标投影转换步骤不可省略,否则如果底图不是WGS84地理坐标系,点位会出现明显位置偏差
内容的提问来源于stack exchange,提问作者Antonio
相关产品推荐
相关产品推荐

