gpt4 book ai didi

R - tmap 中的多个罗盘

转载 作者:行者123 更新时间:2023-12-05 04:44:36 26 4
gpt4 key购买 nike

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

我知道您可能不太需要,但假设您想展示不同的指南针类型。使用 spData 包中的 nz 我尝试将每个新指南针添加为附加层,但 map 上似乎只包含第一个。

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))

enter image description here

如果未包含 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))

enter image description here

最佳答案

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

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

  1. 制作 5 张 map ,每张都有一个 tmap 圆规。然后使用 tmap::tmap_grob() 函数将这些 map 转换为“grob”对象,以借助 中的 getGrob() 函数提取圆规基础 Rgrid.

  2. 使用 cowplot 库可视化圆规(不带和带标签)

  3. 使用 cowplot 库构建带有五个圆规的最终 map

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

代表

第 1 步 - 从五个“虚拟” map 中提取每个罗盘类型作为“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
)

enter image description here

  • 带有标签
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
)

enter image description here

第 3 步 - 使用五个罗盘绘制 map

# 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
)

enter image description here

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

关于R - tmap 中的多个罗盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69339901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com