R语言绘制空间地图时如何缩放至包含所有点位的目标区域
地图可视化优化方案
问题本质是默认绘图会加载整个卡斯特罗市的全域范围,但房屋点位只占全市很小一块区域,才会出现点位挤成一团看不清的情况,两步就能解决:
- 先算出所有房屋点位的经纬度最大、最小值,往外扩一点距离留边,避免点位紧贴图框
- 画底图的时候直接指定这个经纬度范围,自动裁剪掉没有点位的无关区域,同时把点位改成高对比带描边的样式,就算挨得近也能分清
完整修改后代码
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) shp_subset <- shp[shp$NM_MUNICIP == "CASTRO",] # 房屋点位数据集 Points_properties<-structure(list(Latitude = c(-24.781624, -24.775017, -24.769196, -24.761741, -24.752019, -24.748008, -24.737312, -24.744718, -24.751996, -24.724589, -24.8004, -24.796899, -24.795041, -24.780501, -24.763376, -24.801715, -24.728005, -24.737845, -24.743485, -24.742601, -24.766422, -24.767525, -24.775631, -24.792703, -24.790994, -24.787275, -24.795902, -24.785587, -24.787558, -24.799524), Longitude = c(-49.937369, -49.950576, -49.927608, -49.92762, -49.920608, -49.927707, -49.922095, -49.915438, -49.910843, -49.899478, -49.901775, -49.89364, -49.925657, -49.893193, -49.94081, -49.911967, -49.893358, -49.903904, -49.906435, -49.927951, -49.939603, -49.941541, -49.94455, -49.929797, -49.92141, -49.915141, -49.91042, -49.904772, -49.894034, -49.86651)), row.names = c(NA, -30L), class = c("tbl_df", "tbl","data.frame")) # 计算点位的经纬度边界,加0.01度的缓冲留边 lon_limit <- range(Points_properties$Longitude) + c(-0.01, 0.01) lat_limit <- range(Points_properties$Latitude) + c(-0.01, 0.01) # 按计算好的范围绘制底图,不显示无点位的远郊区域 plot(shp_subset, xlim = lon_limit, ylim = lat_limit, border = "gray40", lwd = 0.8) # 绘制带白边的红色点位,相邻点重叠也能分辨边界 points( x = Points_properties$Longitude, y = Points_properties$Latitude, pch = 21, bg = "#e41a1c", col = "white", lwd = 1.2, cex = 1.3 )
参数调整说明
如果觉得放大倍数不够,就把缓冲值从0.01改小,比如改成0.005,视图会进一步聚焦;觉得太挤就把缓冲值调大。如果需要更清晰的底图显示,也可以把border参数的颜色调浅,避免和点位抢视觉重心。
补充:目前
rgdal包已经停止维护,长期使用建议替换为sf包处理空间数据,读取、绘图的逻辑完全一致,仅需要替换对应函数即可。
内容的提问来源于stack exchange,提问作者Antonio
相关产品推荐
相关产品推荐

