r - 使用缩放的迷你地图绘制地图
问题描述
有谁知道如何绘制与它相邻的缩放区域的地图,类似于:
可以在这里找到一个接近的答案:
但是该解决方案没有显示放大了哪个区域!我想要类似上图的东西,其中那些黄线也指示了缩放区域。这在 R 中是否可行,最好是在 ggplot 中?
解决方案
这是这样做的一种(伪劣)方法。我仍然是 ggplot 业余爱好者......我在 cowplot 中绘制两个 ggplot 对象之间的线时遇到了麻烦,所以我为结果图分配了一个新的坐标系,并手动定义了线的起点和终点。如果有人对如何改进这一点有任何建议,特别是对于以编程方式分配领导线坐标,我会全力以赴。
灵感来自https://r-spatial.org/r/2018/10/25/ggplot2-sf-3.html和https://geocompr.github.io/post/2019/ggplot2-inset-maps/
library(sf)
library(spData)
library(ggplot2)
library(cowplot)
library(rcartocolor)
data("us_states", package = "spData")
north_carolina = read_sf(system.file("shape/nc.shp", package = "sf"))
us_states_2163 = st_transform(us_states, crs = 2163)
north_carolina_2163 = st_transform(north_carolina, crs = 2163)
north_carolina_2163_bb = st_as_sfc(st_bbox(north_carolina_2163))
mainbox <- st_bbox(north_carolina_2163)
# inset panel
inset = ggplot() +
geom_sf(data = us_states_2163, fill = "white") +
geom_sf(data = north_carolina_2163_bb, fill = NA, color = "yellow", size = 1.2) +
theme_void()
# main panel
main = ggplot() +
geom_sf(data = north_carolina_2163, aes(fill = BIR74)) +
scale_fill_carto_c(palette = "Mint") +
geom_sf(data = north_carolina_2163_bb, fill = NA, color = "yellow", size = 1.2) +
theme_void() +
theme(legend.position = "none")
# map
gg_inset_map1 = cowplot::ggdraw() +
coord_equal(xlim = c(0, 20), ylim = c(0, 20), expand = FALSE) +
annotation_custom(ggplotGrob(inset), xmin = 0, xmax = 10, ymin = 10, ymax = 20) +
annotation_custom(ggplotGrob(main), xmin = 0, xmax = 20, ymin = 0, ymax = 10) +
geom_segment(aes(x = 8.9, xend = 19, y = 14.4, yend = 9.2), color = "yellow", size = 1.2) +
geom_segment(aes(x = 7.5, xend = 1, y = 14.4, yend = 9.2), color = "yellow", size = 1.2)
E:我遇到了这个问题和答案,并认为它与这里有关。这是使用 grob 中的数据来定义线条位置的一种方法。但是,正如答案的作者所指出的那样,这是一个“繁琐的......一次性解决方案”。我提交的答案在这方面是相似的……而且比筛选 grob 属性要少得多……
推荐阅读
- git - 提交在本地 Git 中被删除
- javascript - 将 Passport Google 策略身份验证方法从路由移动到控制器中断
- angular - 以非声明方式添加 Angular html 代码
- node.js - 当我与之交互时,Puppeteer(Cluster)关闭页面
- python - 如何通过两个字段添加出现次数的列
- angular - 使用 httpClient 的 post 方法在 url 中传递参数
- javascript - 如何快速生成数万亿个随机 ID
- json - 当我在数据库中保存数据时,我得到的是 id 而不是属性
- java - 尽管调用了 Thread 的 run() 方法,为什么 Java 的 main 方法代码仍然执行?
- kendo-ui - 带有布尔值的剑道网格过滤器模式行