简体   繁体   English

Tmap:自定义连续图例值而不改变比例

[英]Tmap: customize continuous legend values without changing scale

I would like to change the numbers of a legend without changing the scale in tmap.我想在不更改 tmap 中的比例的情况下更改图例的数量。

As an example:举个例子:

r <- raster::raster(matrix(runif(100), 10, 10))
tm_shape(r) +
    tm_raster(legend.is.portrait = FALSE, style = 'cont', title = '', palette = "-RdBu") +
    tm_layout(frame = FALSE, legend.outside = TRUE, legend.outside.position = "bottom")

Gives us a legend that looks like this:给我们一个看起来像这样的图例:

在此处输入图片说明

We can alter the breaks in the tm_raster call like this:我们可以像这样改变 tm_raster 调用中的中断:

tm_shape(r) +
    tm_raster(legend.is.portrait = FALSE, style = 'cont', title = '', palette = "-RdBu",
    breaks = pretty(cellStats(r, range), 3),) +
    tm_layout(frame = FALSE, legend.outside = TRUE, legend.outside.position = "bottom")

This gives us a legend that looks different from the first:这给了我们一个看起来与第一个不同的传说:

在此处输入图片说明

What I want to do is draw the legend in the first example, but only have labels at 0.2, 0.5, and 0.8.我想要做的是在第一个示例中绘制图例,但只有 0.2、0.5 和 0.8 处的标签。

You can pass a labelling function to the legend.format parameter of tm_layout .你可以通过一个标签功能的legend.format的参数tm_layout For example, if you want a wide legend bar with labels at 0.2, 0.5 and 0.8, you create more breaks but only label those that you want:例如,如果您想要一个标签为 0.2、0.5 和 0.8 的宽图例栏,您可以创建更多中断但只标记您想要的那些:

tm_shape(r) +
    tm_raster(legend.is.portrait = FALSE, style = 'cont',
              title = '', palette = "-RdBu", 
              breaks = c(0, 0.2, 0.4, 0.5, 0.6, 0.8, 1)) +
    tm_layout(frame = FALSE, legend.outside = TRUE, 
              legend.outside.position = "bottom",
              legend.format = list(fun = function(x) {
                ifelse(x %in% c(0.2, 0.5, 0.8), x, "")
                }))

在此处输入图片说明

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

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