Edel
Edel

Reputation: 11

R: ggmap How do I add the lon/lat points on my generated map?

I am trying to place data points on a map. But I get the following error: Error in areamap + geom_point(data = d, mapping = aes(x = lon, y = lat), : non-numeric argument to binary operator In addition: Warning message: Incompatible methods ("Ops.raster", "+.gg") for "+"

I have searched the entire platform for a solution, but so far failed to project the points on the generated map. My script in case you can help me:

library(ggmap)
library(ggplot2)
> area <- read.csv("area.csv", head = T, sep = ",")   
> area #area.csv
    species      lat       lon
1 primavera 20.64122 -103.5897
2 primavera 20.60023 -103.5341
3 primavera 20.62951 -103.5282
4 primavera 20.64517 -103.6449
5 primavera 20.68247 -103.5474
> save(area, file="area.rda")
> areabox <- make_bbox(lat = lat, 
+                      lon = lon, 
+                      data = area, 
+                      f = 1)
> areabox
      left     bottom      right        top 
-103.76159   20.51799 -103.41154   20.76472 
> # Ver los registros
> fix(area)
> areamapa <- get_map(location = areabox, 
+                         source = "stamen", 
+                         maptype = "terrain")
> d<-data.frame(lat=c(20.65728,20.70386),
+               lon=c(-103.62736,-103.53156))
> d # puntos
       lat       lon
1 20.65728 -103.6274
2 20.70386 -103.5316
> #incluir puntos georreferenciados
> points<- areamapa + 
+   geom_point(data = d,
+              mapping = aes(x = lon, y = lat), 
+              size = 2, 
+              colour = "red")
Error in areamapa + geom_point(data = d, mapping = aes(x = lon, y = lat),  : 
  non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("Ops.raster", "+.gg") for "+" 

Upvotes: 1

Views: 44

Answers (1)

Ben
Ben

Reputation: 30494

When you call get_map it returns a ggmap object (classed raster object). To plot it, try using ggmap(areamapa). Here is the complete example with points added:

library(ggmap)
library(ggplot2)

area <- read.table(text =
"species      lat       lon
 primavera 20.64122 -103.5897
 primavera 20.60023 -103.5341
 primavera 20.62951 -103.5282
 primavera 20.64517 -103.6449
 primavera 20.68247 -103.5474", header = T)

areabox <- make_bbox(lat = lat, lon = lon, data = area, f = 1)
areamapa <- get_map(location = areabox, source = "stamen", maptype = "terrain")

d <- data.frame(lat = c(20.65728,20.70386), 
                lon = c(-103.62736,-103.53156))

ggmap(areamapa) + 
  geom_point(data = d, mapping = aes(x = lon, y = lat), size = 2, colour = "red")

Output

map with two points using ggmap

Upvotes: 0

Related Questions