简体   繁体   English

tmap:在光栅 map 的图例中围绕键添加矩形框

[英]tmap: add rectangular frame around keys in legend of raster map

I need to place a fill-legend for categorical raster data, where white is used to represent ice, on a raster map on white background.我需要在白色背景上的栅格 map 上放置分类栅格数据的填充图例,其中白色用于表示冰。 I would like to use tmap.我想使用 tmap。 Here is a minimum working example that is similar in spirit to my current project.这是一个与我当前项目在精神上相似的最小工作示例。

library(tmap)
library(dplyr)
library(forcats)
library(stars)
data(land)
land_copy <-
  land %>%
  mutate(cover_simplified =
           fct_other(cover_cls, keep = c("Snow/ice", "Water")))
tm_shape(land_copy) +
  tm_raster(col = "cover_simplified",
            palette = c("white", "lightblue", "brown")) +
  tm_layout(legend.outside = TRUE)

map produced with tmap, no rectangular frame around legend key map 使用 tmap 生成,图例键周围没有矩形框

The problem is that it is unclear that "Snow/ice" in the legend refers to a category because the key colour is invisible on a white background.问题是图例中的“Snow/ice”指的是一个类别不清楚,因为关键颜色在白色背景上是看不见的。 For this reason, "Snow/ice" looks like a legend title rather than a legend key.出于这个原因,“Snow/ice”看起来更像是一个图例标题而不是图例键。 I am aware that I can change the legend background with tm_layout(legend.bg.color = "gray") , but I prefer to keep the background white.我知道我可以使用tm_layout(legend.bg.color = "gray")更改图例背景,但我更喜欢将背景保持为白色。 Instead I would like to draw a thin black rectangular frame around each legend key.相反,我想在每个图例键周围绘制一个细长的黑色矩形框。 Here is this idea implemented with ggplot2.这是用 ggplot2 实现的这个想法。

library(ggplot2)
ggplot() +
  geom_stars(aes(x, y, fill = cover_simplified), data = land_copy) +
  scale_fill_discrete(type = c("white", "lightblue", "brown")) +
  coord_sf() +
  theme_void() +
  theme(legend.key = element_rect(color = "black"))

map produced with ggplot2 with rectangular frame around each legend key map 使用 ggplot2 生成,每个图例键周围有矩形框

I would prefer to work with tmap instead of ggplot2 because the actual map I want to produce is more complex than the example above, so tmap is a better tool for my task.我更愿意使用 tmap 而不是 ggplot2,因为我想要生成的实际 map 比上面的示例更复杂,因此 tmap 是完成我的任务的更好工具。 I am posting this question because I cannot find a function argument for tm_layout() that seems relevant for my problem.我发布这个问题是因为我找不到似乎与我的问题相关的tm_layout()的 function 参数。 A Google search has not led me to useful information either.谷歌搜索也没有给我带来有用的信息。

Is there a way to produce the equivalent of ggplot2's theme(legend.key = element_rect(color = "black") with tmap?有没有办法用 tmap 生成 ggplot2 的theme(legend.key = element_rect(color = "black")

There doesn't seem to be an option for doing this directly in tmap .似乎没有直接在tmap中执行此操作的选项。 However, a tmap is drawn with grid graphics, so it is possible to capture the legend, change it, and draw it back.然而, tmap是用网格图形绘制的,因此可以捕获图例,更改它,然后将其绘制回来。 If you add the following lines after your code:如果您在代码后添加以下行:

g <- grid::grid.grab()
leg <- grid::getGrob(g, "legend")
leg$children[[2]]$children[[2]]$children[[1]]$gp$col <- "black"
leg$children[[2]]$children[[2]]$children[[1]]$gp$lwd <- 1
grid::seekViewport("outside_legend")
grid::grid.draw(leg)

You get:你得到:

在此处输入图像描述

If you want to save this as a pdf, you can't use tmap_save , since this is no longer a tmap object. Instead, get the plot looking as you would like, then do:如果您想将其保存为 pdf,则不能使用tmap_save ,因为这不再是tmap object。相反,按照您的意愿获取 plot,然后执行以下操作:

g <- grid::grid.grab()

pdf("my_pdf.pdf")
grid::grid.newpage()
grid::grid.draw(g)
dev.off()

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

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