简体   繁体   English

如何放大 map 的多个点并将它们全部包含在单独的面板中?

[英]How to zoom in on multiple points of a map and include them all in separate panels?

I have multiple polygons and a raster that make up my map. I would like to "zoom" into each of these polygons in order to better visualize the raster information within each of the polygons.我有多个多边形和一个构成我的 map 的栅格。我想“放大”这些多边形中的每一个,以便更好地可视化每个多边形中的栅格信息。 I figure i can eventually use some combination of facet_grid and ggdraw for the final map, but for now I need help creating the "zoomed in" portion of the map.我想我最终可以将facet_gridggdraw的某种组合用于最终的 map,但现在我需要帮助创建 map 的“放大”部分。

Example:例子:

#Raster layer
library(terra)
library(raster)
library(sf)

f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)

#polygons
v <- vect(system.file("ex/lux.shp", package="terra"))
v <- v[c(1:10)] 

#Base plot
ggplot() +
  geom_raster(data = r, aes(x = x, y = y, fill = elev)) +
  geom_sf(data = v, fill = NA, col = "red") 

I know both the raster and terra packages have a zoom function, but i'm unable to use it with ggplot.我知道raster和地形包都有一个zoom terra ,但我无法将它与 ggplot 一起使用。 Any guidance would be welcome, thanks!欢迎任何指导,谢谢!

I couldn't get your example data to work, however you can zoom in on portions of a ggplot using either:我无法让您的示例数据正常工作,但是您可以使用以下任一方式放大ggplot的部分:

your_plot+
   coord_cartesian(
      xlim = c(xmin, xmax), 
      ylim = c(ymin, ymax)
   )

or或者

your_plot+
   xlim(xmin, xmax)+
   ylim(ymin, ymax)

or或者

your_plot+
   scale_x_continuous(limits = c(xmin, xmax))+
   scale_y_continuous(limits = c(ymin, ymax))

Be aware that the latter 2 examples actually 'cut off' data points outside the 'zoomed' area, so for example lines or polygons which extend beyond the zoomed area will look different.请注意,后两个示例实际上“切断”了“缩放”区域之外的数据点,因此例如延伸到缩放区域之外的线或多边形看起来会有所不同。 coord_cartesian doesn't cut points off, and in this instance is probably what you want. coord_cartesian不会切断点,在这种情况下可能是你想要的。 This is explained nicely in the ggplot2 cheatsheet found here这在此处找到的 ggplot2 备忘单中得到了很好的解释

在此处输入图像描述

Additionally, if you are plotting an sf object using geom_sf() , you can also use coord_sf() , which is designed to handle spatial data, and also does not 'cut off' data which extends outside the plot area.此外,如果您正在使用geom_sf()绘制sf object ,您还可以使用coord_sf() ,它旨在处理空间数据,并且不会“切断”延伸到 plot 区域之外的数据。

your_plot+
   coord_sf(
      xlim = c(xmin, xmax), 
      ylim = c(ymin, ymax)
   )

One tip: make sure the min value is less than the max value when specifying the limits.一个提示:在指定限制时确保min小于max This might sound obvious, but for example here in Australia our latitudes ( y values) are all negative.这听起来很明显,但例如在澳大利亚,我们的纬度( y值)都是负数。 Therefore, it's easy to accidently confuse the 'smaller' number with the 'larger' number.因此,很容易不小心将“较小”的数字与“较大”的数字混淆。

One way to arrange your individual zoomed plots into one is by using cowplot::plot_grid ( cowplot documentation here ).将您的各个缩放图合并为一个的一种方法是使用cowplot::plot_grid此处为 cowplot 文档)。 As a very basic example, you simply provide it with ggplot objects you want to arrange together (see documentation)作为一个非常基本的示例,您只需向它提供要排列在一起的ggplot对象(请参阅文档)

plot_grid(p1, p2, p3, p4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM