[英]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 "+"
但是我收到以下错误:areamap + geom_point(data = d,mapping = aes(x = lon,y = lat),:二进制运算符的非数字参数另外:警告消息:不兼容的方法(“Ops. raster", "+.gg") 用于 "+"
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 "+"
When you call get_map
it returns a ggmap
object (classed raster object).当您调用
get_map
时,它会返回一个ggmap
对象(分类栅格对象)。 To plot it, try using ggmap(areamapa)
.要绘制它,请尝试使用
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输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.