在R中使用ggmap添加上下文(或任意)地图插图? [英] Add contextual (or arbitrary) map insets with ggmap in R?
本文介绍了在R中使用ggmap添加上下文(或任意)地图插图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好日子,
假设我在R中使用 library(ggmap)
来制作这张地图:
ggmap(get_map(location = c(lon = -81.38630,lat = 19.30340),source =stamen,maptype = terrain,zoom = 14))
给出:
如何为这张图片添加一个小的上下文插图贴图(可能是左上角?)显示更广泛的地理区域?在这个小插图中,我希望它有一个框,概述大地图适合哪里。
感谢您的帮助。
解决方案
这是一个基于使用 grid
视口的可能解决方案。
library(ggmap)
库(网格)
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)
网格.rect(gp = gpar(col =white,lwd = 5))
popViewport()
Good day,
Let's say I use library(ggmap)
in R to make this map:
ggmap(get_map(location = c(lon = -81.38630, lat = 19.30340), source = "stamen", maptype = "terrain", zoom = 14))
Which gives:
How do I add a small contextual inset map to this image (probably near the top left?) to show the wider geographical area? In this small inset I'd like for it to have a box that outlines where the bigger map fits in.
Thank you for your help.
解决方案
Here is a possible solution based on the use of 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()
这篇关于在R中使用ggmap添加上下文(或任意)地图插图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文