R语言绘制空间图形:添加点位属性标签并放大点位显示区域
R空间地理绘图点位标注与视图聚焦调整方法
以下为两项需求的具体实现,直接在原有代码基础上修改即可:
核心修改逻辑
- 点位属性编号标注:使用基础绘图函数
text(),在点位绘制完成后,将标注位置相对点位坐标做微小偏移,叠加绘制Propertie字段的编号值,避免编号遮挡黑色点位符号。 - 视图范围聚焦:原代码中参数
z控制行政边界裁剪范围向点位集合靠拢的程度,z取值越接近1,最终显示范围越贴合点位分布、视角越近。将原代码中z=0.4调整到0.7~0.9区间即可实现更近的聚焦效果,同时可给点位经纬度范围增加少量缓冲,避免边缘点位贴到图幅边框。
修改后可直接运行的完整代码
library(rgdal) library(sf) library(raster) library(dplyr) 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) shp_subset <- shp[shp$NM_MUNICIP == "CASTRO",] # 点位数据集 Points_properties<-structure(list(Propertie = c(1,2,3,4,5), Latitude = c(-24.781624, -24.775017, -24.769196, -24.761741, -24.752019), Longitude = c(-49.937369, -49.950576, -49.927608, -49.92762, -49.920608)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame")) # 绘图边距设置 marsize <- .2 par(mar = rep(marsize,4)) # 调整z值控制聚焦程度,z越接近1视角越近,此处设为0.8可按需修改 z <- 0.8 # 范围缓冲值,避免点位贴边 buffer_dist <- 0.002 shp_subset_cropped <- raster::crop(shp_subset, extent(c( (extent(shp_subset)@"xmin"*(1-z)+z*(min(Points_properties$Longitude)-buffer_dist)), (extent(shp_subset)@"xmax"*(1-z)+z*(max(Points_properties$Longitude)+buffer_dist)), (extent(shp_subset)@"ymin"*(1-z)+z*(min(Points_properties$Latitude)-buffer_dist)), (extent(shp_subset)@"ymax"*(1-z)+z*(max(Points_properties$Latitude)+buffer_dist)) ))) plot(shp_subset_cropped) # 绘制黑色点位 points(x = Points_properties$Longitude, y= Points_properties$Latitude, col = "black",pch = 16,cex=1) # 标注点位对应属性编号,坐标偏移0.0015度避免遮挡点位 text(x = Points_properties$Longitude + 0.0015, y = Points_properties$Latitude + 0.0015, labels = Points_properties$Propertie, cex = 1, col = "darkred")
原始参考效果图

内容的提问来源于stack exchange,提问作者Antonio
相关产品推荐
相关产品推荐

