我正在尝试使用ggmap和Stamen贴图为我的研究站点制作一张贴图。我已经看到了一些类似的问题,但是还没有找到将解决方案整合到我的Stamen地图代码中的方法。
我对此有两个问题:1.如何自定义标注地图上的点?2.如何在雄蕊地图中添加比例尺? (可以是表示距离的线,也可以是地图上的x cm =现实生活中的y km)
Tcoords <- read.csv("Tcoords.csv")
我的文件看起来像这样
# trap latitude longitude
1 52.34431 0.5374620
2 52.34281 0.5382080
3 52.34468 0.5406787
4 52.34357 0.5398280
5 52.34431 0.5397050
6 52.34516 0.5406294
根据建议,我将结果粘贴到了dput(head(Tcoords))
:
structure(list(trap = c("1", "2", "3", "4", "5", "6"), latitude = c(52.344312,
52.342809, 52.3446849, 52.343572, 52.34431, 52.3451601), longitude = c(0.537462,
0.538208, 0.5406787, 0.539828, 0.539705, 0.5406294)), row.names = c(NA,
6L), class = "data.frame")
这是我用来绘制点的代码
center = c(lon = 0.5406294, lat = 52.3451601)
qmap(center, zoom = 16, source = "stamen", maptype = "watercolor")+
geom_point(aes(x = longitude, y = latitude), size = 4, shape = 21,
fill = "dark green", data = Tcoords)
但是以某种方式,陷阱不会被识别为对象。这可能是基本的东西,但是我不确定我错过了什么(R的新手)。我在这里将“陷阱”保存为文本对象。
感谢您的帮助!
将标签粘贴到地图上只是在geom_text()
函数中重新定义数据源的问题。为了使比例尺可以在地图上打印,只需遵循此问题中的解决方案即可:Is there a way to add a scale bar (for linear distances) to ggmap?
#get base map
map.base <- get_map(location = center, zoom = 16, source = "stamen", maptype = "watercolor") # could also use zoom = "auto"
#get extent of base map
bb <- attr(map.base,"bb")
#define the location and length of scale bar
sbar <- data.frame(lon.start = c(bb$ll.lon + 0.1*(bb$ur.lon - bb$ll.lon)),
lon.end = c(bb$ll.lon + 0.25*(bb$ur.lon - bb$ll.lon)),
lat.start = c(bb$ll.lat + 0.1*(bb$ur.lat - bb$ll.lat)),
lat.end = c(bb$ll.lat + 0.1*(bb$ur.lat - bb$ll.lat)))
#Calculate distance in meters
library(geosphere)
sbar$distance = distGeo(c(sbar$lon.start,sbar$lat.start), c(sbar$lon.end,sbar$lat.end))
map.scale <- ggmap(map.base, extent="device") +
geom_point(aes(x = longitude, y = latitude), size = 4, shape = 21, fill = "dark green", data = Tcoords) +
geom_text(data=Tcoords, aes(label=trap, x = longitude, y = latitude), nudge_x = 0.0001, nudge_y = 0.0001, color="black") +
geom_segment(data = sbar, aes(x = lon.start, xend = lon.end, y = lat.start, yend = lat.end)) +
geom_text(data = sbar, aes(x = (lon.start + lon.end)/2,
y = lat.start + 0.025*(bb$ur.lat - bb$ll.lat),
label = paste(format(distance, digits = 4, nsmall = 2), 'm')),
hjust = 0.5, vjust = 0)
map.scale
可能需要调整geom_text()
功能中的nudge_x和_y以正确放置标签。