简体   繁体   English

如何反转 R Tmap 中发散的调色板?

[英]How to reverse the diverging color palette in R Tmap?

I'm using the R package Tmap to create choropleths of a variable that is sometimes positive and sometimes negative.我正在使用 R 包 Tmap 创建变量的等值线,该变量有时为正,有时为负。 The default diverging color palette shows positive values in green and negative values in red.默认发散调色板以绿色显示正值,以红色显示负值。 I would like to reverse this, to show positive values in red and negative values in green, to conform with my other choropleths that use the sequential palette.我想扭转这一点,用红色显示正值,用绿色显示负值,以符合我使用顺序调色板的其他 choropleths。 I've looked through the manual and tried various things, but I can't seem to get it to work.我翻阅了手册并尝试了各种方法,但似乎无法使其正常工作。 Here is a reproducible example.这是一个可重现的示例。

library(tmap)
data(Europe)
Europe$outcome = c(scale(1:nrow(Europe)))
tm_shape(Europe) +
    tm_polygons(col="outcome",
                style="cont")

I've tried "auto.palette.mapping=F", "fill.palette="-div"", "fill.palette="YlGnBu"", etc, but I can't seem to change anything.我试过“auto.palette.mapping=F”、“fill.palette="-div"”、“fill.palette="YlGnBu"”等,但我似乎无法改变任何东西。

You should specify the layout:您应该指定布局:

library(tmap)
data(Europe)
Europe$outcome = c(scale(1:nrow(Europe)))
tm_shape(Europe) +
  tm_polygons(col = "outcome",
              style ="cont", palette = "seq") +
  tm_layout(aes.palette = list(seq = "-RdYlGn"))

在此处输入图片说明

tm_shape(Europe) +
  tm_polygons(col = "outcome",
              style ="cont", palette = "seq") +
  tm_layout(aes.palette = list(seq = "RdYlGn"))

在此处输入图片说明

EDIT as suggested by user3386170 in the comments in newer versions of tmap (I tested on 2.3-2, not sure if it was possible when the original answer was posted) the following simplification is available:编辑由user3386170在评论中的较新版本建议tmap (我在2.3-2测试,不知道是否有可能将原来的答案被张贴时)以下的简化是可供选择:

tm_shape(Europe) +
  tm_polygons(col = "outcome",
              style ="cont", palette = "-RdYlGn")

tm_shape(Europe) +
  tm_polygons(col = "outcome",
              style ="cont", palette = "RdYlGn")

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

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