用R加入两个ggmap

时间:2017-08-18 19:17:35

标签: r ggplot2 ggmap

我尝试复制another question中出现的以下图片,但我无法合并两张地图:

我使用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")

1 个答案:

答案 0 :(得分: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()