【问题标题】:Can you stack two different ggmap maps?你能堆叠两个不同的ggmap地图吗?
【发布时间】: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 有什么解决办法吗?

【问题讨论】:

标签: r ggplot2 ggmap


【解决方案1】:

这是一个模拟alpha = .5的可重现示例:

library(ggmap)
bbox <- c(left = -97.132, bottom = 31.536, right = -97.105, top = 31.560)
m1 <- get_stamenmap(bbox, maptype = "watercolor", zoom = 13)
m2 <- get_stamenmap(bbox, maptype = "terrain-labels", zoom = 13)
m2_ <- adjustcolor(m2, alpha.f = .5)
attributes(m2_) <- attributes(m2)
fourCorners <- expand.grid(lon = as.numeric(attr(m1, "bb")[, c("ll.lon", "ur.lon")]), lat = as.numeric(attr(m1, "bb")[, c("ll.lat", "ur.lat")]))
xmin <- attr(m1, "bb")$ll.lon
xmax <- attr(m1, "bb")$ur.lon
ymin <- attr(m1, "bb")$ll.lat
ymax <- attr(m1, "bb")$ur.lat
ggplot() + 
  geom_blank(aes(x = lon, y = lat), data = fourCorners) + 
  annotation_raster(m1, xmin, xmax, ymin, ymax) + 
  annotation_raster(m2_, xmin, xmax, ymin, ymax)

【讨论】:

  • 另一种选择可能是预先合并两个栅格。使用您最喜欢的搜索引擎(例如“merge rasters in r”),您会发现很多选项...
  • 太棒了,我会试试的!
  • 我试过 Luke 的代码,但我只得到一个空网格,X 轴和 Y 轴上的地理坐标...
  • 很抱歉听到这个消息。在这里,该示例按预期工作。也许更新软件包会有所帮助。 (我当然假设你做了m1 &lt;- newmap1;m2 &lt;- newmap2 或者你用 newmap1 替换了 m1 等)
猜你喜欢
  • 1970-01-01
  • 2014-02-07
  • 2015-01-14
  • 2017-01-29
  • 2021-03-06
  • 2019-11-10
  • 2017-04-06
  • 2016-08-01
  • 1970-01-01
相关资源
最近更新 更多