首页 > 解决方案 > R - tmap 中的多个指南针

问题描述

tm_compass()是否可以在由创建的地图上包含多个tmap

我知道您可能不太可能需要,但说您想炫耀不同的指南针类型。nz从包中使用spData我尝试将每个新指南针添加为附加层,但似乎只有第一个包含在地图上。

library(spData)
library(tmap)

tm_shape(nz)+
  tm_fill()+
  tm_compass(type = 'arrow', position = c(0.1, 0.9))+
  tm_compass(type = '4star', position = c(0.1, 0.8))+
  tm_compass(type = '8star', position = c(0.1, 0.7))+
  tm_compass(type = 'radar', position = c(0.1, 0.6))+
  tm_compass(type = 'rose', position = c(0.1, 0.5))

在此处输入图像描述

如果arrow不包括在内,则4star取而代之:

tm_shape(nz)+
  tm_fill()+
  # tm_compass(type = 'arrow', position = c(0.1, 0.9))+
  tm_compass(type = '4star', position = c(0.1, 0.8))+
  tm_compass(type = '8star', position = c(0.1, 0.7))+
  tm_compass(type = 'radar', position = c(0.1, 0.6))+
  tm_compass(type = 'rose', position = c(0.1, 0.5))

在此处输入图像描述

标签: rtmap

解决方案


有趣的问题。正如您所指出的,在正常使用中,不太可能需要为同一张地图显示多个指南针,这可能就是tmap库的默认行为不处理这种情况的原因。

也就是说,仍然可以tmap使用一些变通方法将所有五个指南针添加到同一张地图!所以,请在下面找到一般的“策略”:

  1. 构建 5 张地图,每张地图都有一个tmap指南针。然后使用函数将这些地图转换为“grob”对象,以借助库中的函数tmap::tmap_grob()提取指南针。getGrob()base Rgrid

  2. cowplot使用库可视化指南针(不带标签和带标签)

  3. cowplot使用图书馆用五个指南针构建最终地图

注意:当运行下面的reprex时,不要担心将显示在您的不同绘图plotting device的渲染(因为渲染取决于设备的纵横比);重要的是保存在.png文件中的地图的渲染。

代表

第 1 步 - 从五个“虚拟”地图中提取每个指南针类型作为“GROB”对象

library(tmap)
library(spData)
library(grid)

# Get a list named 'maps' containing 5 maps, each one with a different compass
compass_type <- c("arrow", "4star", "8star", "radar", "rose")

maps <- lapply(compass_type,
               function(x)
                 tm_shape(nz) +
                 tm_fill() +
                 tm_compass(type = x, position = c(0.1, 0.7)))


# Get a list named 'compasses' containing the 5 compasses as 'grob' objects 
compasses <- lapply(maps, function(x) getGrob(tmap_grob(x), gPath("compass")))

第 2 步 - 五个tmap罗盘的可视化

  • 无标签
library(cowplot)

compasses_only <- plot_grid(plotlist = compasses,
                            nrow = 3,
                            ncol = 2,
                            scale = 0.8, 
                            byrow = TRUE)

compasses_only

# Save the plot (need to install the 'rstudioapi' library)
# NB: adjust 'width' and 'height' params to get the best possible rendering 
rstudioapi::savePlotAsImage(
  "tmap_compasses.png", # add the path if different of the working directory   
  format = "png", # other possible formats: "jpeg", "bmp", "tiff", "emf", "svg", "eps"
  width = 780,
  height = 965
)

在此处输入图像描述

  • 带标签
compasses_only_labeled <- plot_grid(plotlist = compasses,
                            nrow = 3,
                            ncol = 2,
                            scale = 0.8, 
                            labels = compass_type,
                            label_size = 10,
                            label_x = c(0.42, 0.43, 0.43, 0.42, 0.44),
                            label_y = 0.1,
                            byrow = TRUE)

compasses_only_labeled

# Save the plot (need to install the 'rstudioapi' library)
# NB: adjust 'width' and 'height' params to get the best possible rendering 
rstudioapi::savePlotAsImage(
  "tmap_compasses_labeled.png",  
  format = "png", 
  width = 780,
  height = 965
)

在此处输入图像描述

第 3 步 - 使用五个指南针构建地图

# Get a 'nz' map (without any compass) as 'grob' object 
nz_map <- tmap_grob(tm_shape(nz) + tm_fill())

# Convert 'compasses_only" into 'grob' object
compasses_only <- cowplot::as_grob(compasses_only)

# Build the map with the 5 compasses
map_with_compasses_2 <- ggdraw() +
  draw_grob(nz_map) +
  draw_grob(compasses_only,
            width = 0.23, height = 0.65,
            x = 0.28, y = 0.35,
            hjust = 0,
            vjust = 0,
            halign = 0.5,
            valign = 0.5,
            scale = 0.8)

map_with_compasses_2

# Save the plot (need to install the 'rstudioapi' library)
# NB: adjust 'width' and 'height' params to get the best possible rendering 
rstudioapi::savePlotAsImage(
  "map_with_compasses_2.png",   
  format = "png", 
  width = 1500,
  height = 900
)

在此处输入图像描述

reprex 包(v2.0.1)创建于 2022-01-31


推荐阅读