使用缩放的迷你地图绘制地图
Plotting a map with a zoomed mini map
有谁知道如何绘制一张地图,其中有一个类似这样的放大区域:
可以在这里找到一个接近的答案:
但该解决方案并未显示放大了哪个区域!我想要类似上图的东西,其中缩放区域也由那些黄线表示。这在 R 中是否可行,最好使用 ggplot?
这是一种(粗制滥造的)方法。我仍然是一个 ggplot 业余爱好者...我在两个 ggplot 对象之间的 cowplot 中画线时遇到了问题,所以我为生成的图分配了一个新的坐标系,并手动定义了线的起点和终点。如果有人对如何改进这一点有任何建议,特别是对于以编程方式分配引导线坐标,我会洗耳恭听。
灵感来自https://r-spatial.org/r/2018/10/25/ggplot2-sf-3.html and 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 属性要少得多……
有谁知道如何绘制一张地图,其中有一个类似这样的放大区域:
可以在这里找到一个接近的答案:
但该解决方案并未显示放大了哪个区域!我想要类似上图的东西,其中缩放区域也由那些黄线表示。这在 R 中是否可行,最好使用 ggplot?
这是一种(粗制滥造的)方法。我仍然是一个 ggplot 业余爱好者...我在两个 ggplot 对象之间的 cowplot 中画线时遇到了问题,所以我为生成的图分配了一个新的坐标系,并手动定义了线的起点和终点。如果有人对如何改进这一点有任何建议,特别是对于以编程方式分配引导线坐标,我会洗耳恭听。
灵感来自https://r-spatial.org/r/2018/10/25/ggplot2-sf-3.html and 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:我 运行 进入这个