Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggmap

I want to create a map using ggmap. I want to show the location of some points, starting with a dataframe with the UTM coordinates. However, I always end up with the Error message: 'Error: ggplot2 doesn't know how to deal with data of class SpatialPointsDataFrame'. Help is very much appriciated... o_0

Here is my example:

#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
like image 336
Anja Avatar asked Dec 06 '25 14:12

Anja


1 Answers

Extract the coordinated from the SpatialPointsDataFrame

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

Note - the two points you give are off the edge of the map.

like image 134
Richard Telford Avatar answered Dec 08 '25 06:12

Richard Telford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!