The following code allows me to draw a polygon on a map and find spatial
points that fall inside the polygon:

library(sp); library(raster)
coordsDF <- data.frame(coordsx=c(85.1159641, 119.3074835, 32.2725258,
0.1943375), coordsy=c(25.45215,  58.44791, -32.47434, 57.50565))
coordinates(coordsDF) <- c('coordsx', 'coordsy')
crs.geo <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
proj4string(coordsDF) <- crs.geo
plot(coordsDF, pch = 20, cex=2, col = "steelblue")
data(wrld_simpl)
plot(wrld_simpl, add = T)
myPolygon <- drawPoly()
proj4string(myPolygon) <- crs.geo
findPoints <- as.data.frame(over(myPolygon, coordsDF, returnList = T))

However if I try to do this over a google map plotted using ggmap/ggplot2, I
get the error 'invalid graphics state':
  
library(ggmap); library(ggplot2)
coordsDF <- as.data.frame(coordsDF)
myMap  <-  get_map(location = 'UK', zoom = 3, maptype = "hybrid")
myMap <- ggmap(myMap, extent = "panel", legend = "right")
myMap + geom_point(data = coordsDF, aes(coordsx, coordsy), colour = "red",
size=10)
myPolygon <- drawPoly()

Is it possible for drawPoly() to work over a google map using ggmap/ggplot2?

Alternatively, can I use drawPoly() over a google map downloaded with a
different R package?

Thanks




	[[alternative HTML version deleted]]

