gpt4 book ai didi

r - 在 R 中使用 ggmap 添加上下文(或任意) map 插图?

转载 作者:行者123 更新时间:2023-12-02 09:15:59 25 4
gpt4 key购买 nike

你好,

假设我在 R 中使用 library(ggmap) 制作这张 map :

ggmap(get_map(location = c(lon = -81.38630, lat = 19.30340), source = "stamen", maptype = "terrain", zoom = 14))

给出:

enter image description here

如何向该图像添加一个小的上下文插图(可能靠近左上角?)以显示更广泛的地理区域?在这个小插图中,我希望它有一个框来勾勒出较大 map 适合的位置。

感谢您的帮助。

最佳答案

这是一个基于使用 grid viewports 的可能解决方案。

library(ggmap)
library(grid)

map1 <- get_map(location = c(lon = -81.38630, lat = 19.30340),
maptype = "terrain", zoom = 14)
map2 <- get_map(location = c(lon = -81.38630, lat = 19.30340),
maptype = "terrain", zoom = 12)

p1 <- ggmap(map1)
g1 <- ggplotGrob(p1)
grid.draw(g1)

pushViewport( viewport(x=0.25, y=0.8, w=.3, h=.3) )
xy <- data.frame(x=c(-81.41,-81.41,-81.36,-81.36,-81.41),
y=c(19.33,19.28,19.28,19.33,19.33))
p2 <- ggmap(map2) +
geom_path(data=xy, aes(x,y), color="red", lwd=1) +
theme_void()
g2 <- ggplotGrob(p2)
grid.draw(g2)
grid.rect(gp=gpar(col="white", lwd=5))
popViewport()

enter image description here

关于r - 在 R 中使用 ggmap 添加上下文(或任意) map 插图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47183172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com