【问题标题】:Plotting a map with a zoomed mini map使用缩放的迷你地图绘制地图
【发布时间】:2021-09-15 04:28:30
【问题描述】:

有谁知道如何绘制一张地图,其附近有一个类似这样的缩放区域:

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

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

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

【问题讨论】:

标签: r ggplot2 spatial


【解决方案1】:

这是一种(伪劣)方法。我仍然是一个 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)

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

【讨论】:

    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多