ggmap叠加LSOA shp文件报错:找不到lon对象的技术求助
问题:ggmap叠加LSOA shp文件报错“object 'lon' not found”
我正在自学将城镇的ggmap地图与.shp文件叠加,划分出LSOA(下层超级输出区),目的是更清晰地向当地领导展示特定区域、街道名称和地标相关的LSOA内事件,提升沟通效率。
按教程步骤操作:
- 获取Google Maps API并使用qmap创建ggmap地图对象
- 读取LSOA shp文件
- 将两者整合到同一个ggplot绘图中,对齐经纬度美学参数
编写的代码:
londonmap <- qmap("London") LSOA<- readOGR(".", "Lower_Layer_Super_Output_Areas_(December_2011)_Boundaries_Super_Generalised_Clipped_(BSC)_EW_V3") mymap <- londonmap + geom_polygon(aes(x = lon, y = lat), data = LSOA)
运行后报错:
"Error in FUN(X[[i]], ...) : object 'lon' not found"
请问哪里出错了?是不是地图对象和shp文件的经纬度不匹配导致的?
编辑1:dput(londonmap)返回结果:
structure(list(data = structure(list(lon = c(-0.566352679492181, 0.312553570507799, -0.566352679492181, 0.312553570507799), lat = c(51.2324435804282, 51.2324435804282, 51.7794952698017, 51.7794952698017)), out.attrs = list( dim = c(lon = 2L, lat = 2L), dimnames = list(lon = c("lon=-0.5663527", "lon= 0.3125536"), lat = c("lat=51.23244", "lat=51.77950" ))), class = "data.frame", row.names = c(NA, -4L)), layers = list( <environment>, <environment>, <environment>), scales = <environment>, mapping = structure(list(x = ~lon, y = ~lat), class = "uneval"), theme = structure(list(axis.title = structure(list(), class = c("element_blank", "element")), axis.text = structure(list(), class = c("element_blank", "element")), axis.ticks.length = structure(0, unit = 1L, class = c("simpleUnit", "unit", "unit_v2")), panel.background = structure(list(), class = c("element_blank", "element")), panel.spacing = structure(0, unit = 3L, class = c("simpleUnit", "unit", "unit_v2")), panel.grid.major = structure(list(), class = c("element_blank", "element")), panel.grid.minor = structure(list(), class = c("element_blank", "element")), plot.margin = structure(c(0, 0, 0, 0), unit = 3L, class = c("simpleUnit", "unit", "unit_v2")), legend.position = "right"), complete = FALSE, validate = TRUE), coordinates = <environment>, facet = <environment>, plot_env = <environment>, labels = list(x = "lon", y = "lat", xmin = "xmin", xmax = "xmax", ymin = "ymin", ymax = "ymax")), class = c("gg", "ggplot" ))
编辑2:dput(LSOA)输出内容过大,无法粘贴。
解决方案
核心问题
你用readOGR读取的LSOA是SpatialPolygonsDataFrame对象,不是普通数据框,没有直接的lon和lat列。geom_polygon无法直接识别这类空间对象的坐标信息,所以报错找不到lon。
修复步骤
推荐两种处理方式,优先选择方法2(当前空间数据处理主流方案):
方法1:用fortify转换为数据框(旧方法)
library(ggplot2) library(rgdal) londonmap <- qmap("London") LSOA <- readOGR(".", "Lower_Layer_Super_Output_Areas_(December_2011)_Boundaries_Super_Generalised_Clipped_(BSC)_EW_V3") # 将空间对象转换为ggplot兼容的数据框 LSOA_df <- fortify(LSOA) # 叠加绘图,必须指定group参数保证多边形正确绘制 mymap <- londonmap + geom_polygon(data = LSOA_df, aes(x = long, y = lat, group = group), fill = NA, color = "red", size = 0.5) print(mymap)
方法2:用sf包读取shp(推荐,更简洁)
sf包是R空间数据处理的标准工具,与ggplot2兼容性更好:
library(ggplot2) library(ggmap) library(sf) londonmap <- qmap("London") # 用sf读取shp文件 LSOA_sf <- st_read(".", "Lower_Layer_Super_Output_Areas_(December_2011)_Boundaries_Super_Generalised_Clipped_(BSC)_EW_V3") # 直接用geom_sf叠加,inherit.aes=FALSE避免映射冲突 mymap <- londonmap + geom_sf(data = LSOA_sf, fill = NA, color = "red", inherit.aes = FALSE) print(mymap)
额外注意点
- 检查坐标参考系(CRS):ggmap的地图使用WGS84(EPSG:4326),如果你的LSOA shp文件不是这个CRS,用
st_transform(LSOA_sf, 4326)转换。 - 调整样式:可以修改
fill、color、size参数,让LSOA边界更符合展示需求。
内容的提问来源于stack exchange,提问作者SOFaddict
相关产品推荐
相关产品推荐

