2017-08-18 42 views
0

我试图复制下面的图像中出现的another question,但我不能合并两个地图:连接两个ggmap有R

我用annotation_custom,但它不工作:

library(raster);library(ggmap) 
library(ggplot2) 
library(ggthemes) 

g_canarias <- ggplotGrob(
    ggmap(get_map(location = c(lon = mean(c(-18.3, -13.15)), 
          lat = mean(c(27.5, 29.55))), 
       zoom = 6, 
       maptype = "satellite")) + 
    scale_x_continuous(limits = c(-18.3, -13.15), expand = c(0, 0)) + 
    scale_y_continuous(limits = c(27.5, 29.55), expand = c(0, 0)) + 
    theme_bw() + 
    theme(legend.position = "none", 
    plot.background = element_rect(colour = "black"), 
    # plot.margin = margin(0, 0, 0, 0), 
    axis.title.x = element_blank(), 
    axis.title.y = element_blank()) 
) 

ggmap(get_map(location = c(lon = mean(c(-9.5, 4.5)), 
         lat = mean(c(35, 44))), 
      zoom = 6, 
      maptype = "satellite")) + 
    scale_x_continuous(limits = c(-9.5, 4.5), expand = c(0, 0)) + 
    scale_y_continuous(limits = c(35, 44), expand = c(0, 0)) + 
    xlab("Longitud") + ylab("Latitud") + 
    theme_bw() + 
    theme(legend.position = "none") 
+0

例谷歌图像https://www.google.es/imgres?imgurl=https%3A%2F%2Fi.stack.imgur.com%2FJb21a .JPG&imgrefurl = HTTPS%3A%2F%2Fstackoverfl ow.com%2Fquestions%2F39067838%2Fparsimonious路到加 - 北 - 箭头和大规模杆到ggmap&的docID = Mw8VBMZyDtTvrM&tbnid = MpneWEqjUzX0VM%3A&兽医= 10ahUKEwj3xebUt-HVAhUKfRoKHbASBc8QMwhiKDEwMQ..i&W = 813&H = 862&波黑= 662&BIW = 1366&Q = ADD%20ggmap%20in%20other%20ggmap&ved = 0ahUKEwj3xebUt-HVAhUKfRoKHbASBc8QMwhiKDEwMQ&iact = mrc&uact = 8 – Mario

+0

您可以添加一个链接,指出您要重现的问题。随着'库(ggmap)'的加入,第一个情节呈现,但第二个情节没有;什么数据是'西班牙' - 你能添加一个例子吗? – user20650

+0

你有[link](https://www.google.es/imgres?imgurl=https%3A%2F%2Fi.stack.imgur.com%2FJb21a.jpg&imgrefurl=https%3A%2F%2Fstackoverflow.com%2Fquestions %2F39067838%2Fparsimonious路到加 - 北 - 箭头和大规模杆到ggmap&的docID = Mw8VBMZyDtTvrM&tbnid = MpneWEqjUzX0VM%3A&兽医= 10ahUKEwj3xebUt-HVAhUKfRoKHbASBc8QMwhiKDEwMQ..i&W = 813&H = 862&波黑= 662&BIW = 1366&q = ADD%20ggmap% 20in%20other%20ggmap&ved = 0ahUKEwj3xebUt-HVAhUKfRoKHbASBc8QMwhiKDEwMQ&iact = mrc&uact = 8)听到 – Mario

回答

1

下面是一个抽象的例子(如你是不可再现的):

library(ggmap) 
hdf <- get_map("houston, texas") 
bb <- unlist(attr(hdf, "bb")) 
bb <- c(bb[2], bb[2]+diff(bb[c(2,4)])/3, bb[3]-diff(bb[c(1,3)])/3, bb[3]) 
ggmap(hdf, extent = "normal") + 
    annotation_raster(hdf, bb[1], bb[2], bb[3], bb[4]) + 
    annotate("rect", xmin=bb[1], xmax=bb[2], ymin=bb[3], ymax=bb[4], fill=NA, color="black", size=1) + 
    coord_quickmap()