SpatialPointsDataframe

SpatialPointsDataframe

我想使用 ggmap 创建 map 。我想显示一些点的位置,从带有 UTM 坐标的数据框开始。但是,我总是以错误消息结束:“错误:ggplot2 不知道如何处理 SpatialPointsDataFrame 类的数据”。非常有帮助... o_0

这是我的例子:

#packages
library(rgeos)
library(maptools)
library(rgdal)
library(sp)
library(ggmap)

#create data frame with UTM coordinates
Point_ID <- c(1,2)
Easting <- c(519769,518250)
Northing <- c(5767155,5766640)

df <- data.frame(Point_ID, Easting, Northing)

#set spatial coordinates to create a Spatial object
myvars <- c("Easting","Northing")
coords <- df[myvars]
sp = SpatialPoints(coords)

spdf = SpatialPointsDataFrame(sp, df)

#define the projection (UTM coordinates zone 30)
proj4string(spdf)=CRS("++proj=utm +zone=30")

#transformed to geographic coordinates (latitude/longitude)
spdf<- spTransform(spdf, CRS("+proj=longlat +datum=WGS84"))


#create map
myLocation <- "Hereford"
myMap <- get_map(location = myLocation, zoom=16, maptype= "satellite")

ggmap(myMap)+
  geom_point(aes(x = lon, y = lat), data = spdf,
             alpha = .5, color="darkred", size = 3)

#Error: ggplot2 doesn't know how to deal with data of class      SpatialPointsDataFrame

最佳答案

从 SpatialPointsDataFrame 中提取坐标

ggmap(myMap)+
geom_point(aes(x = Easting, y = Northing), data = as.data.frame(coordinates(spdf)),
         alpha = .5, color="darkred", size = 3)
注意 - 您给出的两个点不在 map 的边缘。

关于r - ggmap : 'Error: ggplot2 doesn' t know how to deal with data of class SpatialPointsDataFrame',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36372447/

10-11 08:24