2016-04-02 50 views
2

我想使用ggmap創建一個映射。我想顯示一些點的位置,從具有UTM座標的數據框開始。但是,我總是以錯誤消息結尾:'錯誤:ggplot2不知道如何處理類SpatialPointsDataFrame的數據'。幫助非常大大appriciated ... O_0ggmap:'錯誤:ggplot2不知道如何處理類SpatialPointsDataFrame的數據'

這是我的例子:

#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 

回答

5

摘自SpatialPointsDataFrame

ggmap(myMap)+ 
geom_point(aes(x = Easting, y = Northing), data = as.data.frame(coordinates(spdf)), 
     alpha = .5, color="darkred", size = 3) 

注意協調 - 這兩點你給是關閉的邊緣地圖。

相關問題