【发布时间】:2017-05-15 11:52:20
【问题描述】:
我有一个项目,我在使用 ggmap 获得的地图上绘制了一些数据。一切都很好,除了你可以获得的地图品种都不能完全满足我的需要。理想情况下,我希望将两种雄蕊地图类型“水彩”和“地形标签”融合在一起,然后再将它们用作使用 ggplot 绘图的背景。我可以通过以下方式获得 2 个地理数学地图:
library("ggmap")
library("ggplot2")
lon<-c(-71.50,-71.60)
lat<-c(42.40,42.50)
coords<-data.frame(lon,lat)
newmap1<-get_stamenmap(bbox = c(left = -71.63, bottom = 42.36,
right = -71.10, top = 42.55), maptype="watercolor", zoom=13)
newmap2<-get_stamenmap(bbox = c(left = -71.63, bottom = 42.36,
right = -71.10, top = 42.55), maptype="terrain-labels", zoom=13)
bbleft, bbright, bbbottom, bbtop 表示任意一组有意义的坐标。 现在我想将 newmap2 堆叠在 newmap1 上(例如,使用 alpha 0.5),然后使用它来绘制我的数据,就像我通常对单个地图所做的那样:
ggmap(newmap1) + geom_point(aes(x = lon, y = lat), data = coords, colour = "#f409d8", size = 1, alpha =0.8)
谢谢。
我在下面尝试了卢克的代码,但这是我得到的图像: enter image description here
Luke:我现在更新了所有的包,你的代码(转移到我的数据中)可以正常工作了(非常感谢你!!!):
library("ggmap")
library("ggplot2")
lon<-c(-71.50,-71.60)
lat<-c(42.40,42.50)
coords<-data.frame(lon,lat)
newmap1<-get_stamenmap(bbox = c(left = -71.63, bottom = 42.36, right = -71.10, top = 42.55), maptype="watercolor", zoom=12)
newmap2<-get_stamenmap(bbox = c(left = -71.63, bottom = 42.36, right = -71.10, top = 42.55), maptype="toner-lite", zoom=12)
newmap2_ <- adjustcolor(newmap2, alpha.f = .5)
attributes(newmap2_) <- attributes(newmap2)
map <- expand.grid(lon = as.numeric(attr(newmap1, "bb")[, c("ll.lon", "ur.lon")]), lat = as.numeric(attr(newmap1, "bb")[, c("ll.lat", "ur.lat")]))
xmin <- attr(newmap1, "bb")$ll.lon
xmax <- attr(newmap1, "bb")$ur.lon
ymin <- attr(newmap1, "bb")$ll.lat
ymax <- attr(newmap1, "bb")$ur.lat
ggplot () +
geom_blank(aes(x = lon, y = lat), data = map) +
annotation_raster(newmap1, xmin, xmax, ymin, ymax) +
annotation_raster(newmap2_, xmin, xmax, ymin, ymax)
唯一的问题,地图的比例不对,纬度过度拉伸,导致地图看起来不正常: Map showing merging of the two maps 有什么解决办法吗?
【问题讨论】:
-
一个可重复的例子,帮助别人帮助你