2016-12-31 145 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軸上... –

相關問題