简体   繁体   English

R:刻面 map 与 ggplot2

[英]R: Facet a map with ggplot2

I am trying to facet a map with ggplot2 because is faster than tmap package.我正在尝试用 ggplot2 处理 map,因为它比 tmap package 快。 Lamentably the map that I got is not that I have in mind.可悲的是,我得到的 map 并不是我想的那样。

library(ggplot2)
library(raster)
library(ggspatial)


chile  <- getData("GADM",country="Chile",level=1)

chile2= chile[c(2,4:5,7,8,12:16),]
chile2$grupo=1
chile3= chile[c(1,3,6,9:11),]
chile3$grupo=2
mapa=rbind(chile2, chile3)

ggplot() +
  layer_spatial(mapa) +
  lims(x = c( -77.1,-65), y = c(-57, -15))+
  facet_wrap(~grupo)

ggplot 地图

Lamentably the map above is not what I need.可悲的是,上面的 map 不是我需要的。 With tmap I got the map below and is the map that really I need:使用 tmap 我得到了下面的 map 并且是我真正需要的 map:

tm图

Do you know how solve this using ggplot2?你知道如何使用 ggplot2 解决这个问题吗?

This can be done, though I'm not sure if it's really worth the effort.这是可以做到的,但我不确定这是否真的值得付出努力。

There are two issues here:这里有两个问题:

  1. Usually, you can get different scales (latitudes / longitudes in this case) in each facet of facet_wrap by setting scales = "free" , but layer_spatial only works with the coord_sf coordinate system, and coord_sf is hard-coded to only work with fixed scales (there's a discussion on the ggplot GH page that covers how this came about);通常,您可以通过设置scales = "free"facet_wrap的每个方面获得不同的比例(在这种情况下为纬度/经度),但layer_spatial仅适用于coord_sf坐标系,而coord_sf是硬编码的,仅适用于固定比例(在 ggplot GH 页面上有一个讨论,涵盖了这是如何发生的);

  2. Setting lims() explicitly defeats the purpose of free scales anyway.无论如何,设置lims()明确地破坏了自由秤的目的。

For the first issue, we can go against the package developers' intention to define an alternative version of the coordinate system that accepts free scales.对于第一个问题,我们可以 go 反对 package 开发人员的意图,即定义接受自由比例的坐标系的替代版本。 (I'm not saying that it should be done, but it can be. Caveat emptor.) (我并不是说应该这样做,但它可以做到。警告购买者。)

CoordSf2 <- ggproto("CoordSf2",
                    CoordSf,
                    is_free = function() TRUE)
trace(coord_sf, edit = TRUE)

Running the trace(...) line will result in a pop-up window with the code for coord_sf .运行trace(...)行将导致弹出 window 代码为coord_sf Modify the last section from ggproto(NULL, CoordSf, ...) to ggproto(NULL, CoordSf2, ...) will point coord_sf to our modified CoordSf2 instead of the original CoordSf .将最后一部分从ggproto(NULL, CoordSf, ...)修改为ggproto(NULL, CoordSf2, ...)coord_sf指向我们修改后的CoordSf2而不是原来的CoordSf This effect will remain in place until the end of the current R session, or you can terminate it earlier by running untrace(coord_sf) .此效果将一直保持到当前 R session 结束,或者您可以通过运行untrace(coord_sf)提前终止它。

For the second issue, I suppose the limits were set to show only polygons within the limits.对于第二个问题,我想将限制设置为仅显示限制内的多边形。 We can perform this filtering step in the data frame, before passing it to ggplot :我们可以在数据框中执行此过滤步骤,然后将其传递给ggplot

library(dplyr)

keep <- lapply(mapa@polygons, bbox) %>%                    # retrieve bounding box for each polygon
  lapply(function(x) c(x) <= c(-77.1, -57, -65, -15)) %>%  # compare each polygon's bbox against
  lapply(function(x) x == c(F, F, T, T)) %>%               # desired limits, if within, 
  sapply(all)                                              # results should be FFTT

keep # only the 10th polygon is outside the limits
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
[11]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

Now plot with the subset of mapa polygons that fall within the desired limits, with facet scales set to "free" & coord_sf pointing to the modified version that allows it:现在 plot 具有落在所需范围内的mapa多边形子集,刻面比例设置为“自由”和coord_sf指向允许它的修改版本:

ggplot() +
  layer_spatial(mapa[keep, ]) +
  facet_wrap(~grupo, scales = "free")

结果

Personally, though?不过,就个人而言? I'd probably just make separate plots & stitch them together, like the cowplot method demonstrated in this question .我可能只是制作单独的图并将它们缝合在一起,就像这个问题中演示的cowplot方法一样。

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

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