2016-12-31 142 views
0

我有一个项目,我用ggmap绘制的地图上绘制了一些数据。所有的作品都很好,除了你所能获得的地图品种都不能满足我的需求。理想情况下,我希望将两个雄蕊地图类型“水彩画”和“地形标签”融合在一起,然后再将它们用作使用ggplot绘图的背景。你可以堆叠两个不同的ggmap地图吗?

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表明任何一组有意义的坐标:我可以得到2张地理mathcing地图。 现在我想堆newmap2超过newmap1(阿尔法0.5,例如),然后用它来绘制作为我通常会用一个单一的地图做我的数据:

ggmap(newmap1) + geom_point(aes(x = lon, y = lat), data = coords, colour = "#f409d8", size = 1, alpha =0.8) 

谢谢。

我曾尝试下面卢克的代码,但是这是我得到的图像: enter image description here

卢克:我现在更新了所有的包和你的代码(转移到我的数据)的作品(谢谢你合租了好多钱!!!):

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 有什么解决方法吗?

+2

重复的例子WLD帮助别人帮你 – hrbrmstr

+0

[关于如何给一个重复的例子,信息](http://stackoverflow.com /问题/ 5963269 /如何对化妆一个伟大-R-重复性,例如/ 5963610)。 – Jaap

回答

2

这里的模拟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) 

enter image description here

+0

另一种选择是预先合并两个栅格。使用你最喜欢的搜索引擎(例如“合并r中的栅格”),你会发现很多选择...... – lukeA

+0

这太棒了,我会试试! –

+0

我尝试过卢克的代码,但是我只能得到一个空的网格,地理坐标在X轴和Y轴上... –

相关问题