gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:26:35 25 4
gpt4 key购买 nike

我正在使用 R 包 Tmap 创建变量的等值线,该变量有时为正,有时为负。默认发散调色板以绿色显示正值,以红色显示负值。我想扭转这一点,用红色显示正值,用绿色显示负值,以符合我使用顺序调色板的其他 choropleths。我翻阅了手册并尝试了各种方法,但似乎无法使其正常工作。这是一个可重现的示例。

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

我试过“auto.palette.mapping=F”、“fill.palette="-div"”、“fill.palette="YlGnBu"”等,但我似乎无法改变任何东西。

最佳答案

您应该指定布局:

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"))

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

enter image description here

编辑 正如 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")

关于r - 如何反转 R Tmap 中发散的调色板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50954730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com