用放大的迷你地图绘制地图

时间:2021-07-03 17:06:49

标签: r ggplot2 spatial

有谁知道如何绘制一个与它相邻的缩放区域的地图,类似于:

enter image description here

可以在这里找到一个接近的答案:

R - Map Zoom Function, making the plot a circle rather than a square

但是该解决方案没有显示放大了哪个区域!我想要类似上图的东西,其中缩放区域也由那些黄线表示。这在 R 中是否可行,最好使用 ggplot?

1 个答案:

答案 0 :(得分:2)

这是执行此操作的一种(劣质)方法。我仍然是一个 ggplot 业余爱好者......我在两个 ggplot 对象之间的 cowplot 中绘制线条时遇到了麻烦,所以我为结果图分配了一个新的坐标系,并手动定义了线条的起点和终点。如果有人有关于如何改进这一点的建议,特别是对于以编程方式分配引线坐标,我会全力以赴。

灵感来自https://r-spatial.org/r/2018/10/25/ggplot2-sf-3.htmlhttps://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)

ggplot map inset indicator lines

E:我遇到了这个 question and answer 并认为它在这里很重要。这是使用 grob 中的数据来定义线条位置的一种方法。然而,正如答案的作者所指出的,这是一个“繁琐的......一次性解决方案”。我提交的答案在这方面是相似的……而且比筛选 grob 属性要少得多……

相关问题