R - tmap 中的多个罗盘
R - Multiple compasses in tmap
是否可以在 tmap
创建的地图上包含多个 tm_compass()
?
我知道您可能不太需要,但假设您想炫耀不同的指南针类型。使用 spData
包中的 nz
我尝试将每个新指南针添加为附加层,但地图上似乎只包含第一个。
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
圆规。然后使用 tmap::tmap_grob()
函数将这些地图转换为 'grob' 对象,借助 base R
库 grid
中的 getGrob()
函数提取圆规。
使用 cowplot
库
可视化圆规(不带和带标签)
使用 cowplot
库构建带有五个圆规的最终地图
注意:当 运行 下面的 reprex 时,不用担心将在 plotting device
中显示的不同绘图的渲染(因为渲染取决于关于设备的纵横比);重要的是 .png
文件中保存的地图的渲染。
Reprex
第 1 步 - 从五个 'DUMMY' 地图中提取每个罗盘类型作为 '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 package (v2.0.1)
创建于 2022-01-31
是否可以在 tmap
创建的地图上包含多个 tm_compass()
?
我知道您可能不太需要,但假设您想炫耀不同的指南针类型。使用 spData
包中的 nz
我尝试将每个新指南针添加为附加层,但地图上似乎只包含第一个。
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
圆规。然后使用tmap::tmap_grob()
函数将这些地图转换为 'grob' 对象,借助base R
库grid
中的getGrob()
函数提取圆规。使用
可视化圆规(不带和带标签)cowplot
库使用
cowplot
库构建带有五个圆规的最终地图
注意:当 运行 下面的 reprex 时,不用担心将在 plotting device
中显示的不同绘图的渲染(因为渲染取决于关于设备的纵横比);重要的是 .png
文件中保存的地图的渲染。
Reprex
第 1 步 - 从五个 'DUMMY' 地图中提取每个罗盘类型作为 '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 package (v2.0.1)
创建于 2022-01-31