r - 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))
解决方案
有趣的问题。正如您所指出的,在正常使用中,不太可能需要为同一张地图显示多个指南针,这可能就是tmap
库的默认行为不处理这种情况的原因。
也就是说,仍然可以tmap
使用一些变通方法将所有五个指南针添加到同一张地图!所以,请在下面找到一般的“策略”:
构建 5 张地图,每张地图都有一个
tmap
指南针。然后使用函数将这些地图转换为“grob”对象,以借助库中的函数tmap::tmap_grob()
提取指南针。getGrob()
base R
grid
cowplot
使用库可视化指南针(不带标签和带标签)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
推荐阅读
- css - 将 Flex Grid 扩展到容器?
- go - 防止 flag.Parse() 吞下“--”?
- html - 如何强制 CSS 中的图像服从父 flexbox 部分高度?
- java - Spring Security - 指定哪个 X509 可以访问哪个特定端点
- amazon-web-services - Beanstalk:设置 LD_LIBRARY_PATH 变量
- angular - 即使 console.log() 正确打印数据,Angular 8+ *ngFor 也不会在 html 中显示数据
- multidimensional-array - MPI 发送二维数组
- discord - 当我在我的不和谐机器人上使用播放命令时,没有任何反应
- python - Python - 从 UDP 服务器检索数据
- clojure - 如何在第一个真正的谓词匹配的嵌套地图上退出 Clojure walk postwalk?