How to reverse the diverging color palette in R Tmap?
Asked Answered
Q

1

6

I'm using the R package Tmap to create choropleths of a variable that is sometimes positive and sometimes negative. 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. 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.

Quirites answered 20/6, 2018 at 18:19 Comment(0)
C
10

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

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

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:

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

tm_shape(Europe) +
  tm_polygons(col = "outcome",
              style ="cont", palette = "RdYlGn")
Consignment answered 20/6, 2018 at 19:29 Comment(1)
I made a typo there with an extra R, it should have been palette="-RdYlGn". So the whole thing would be tm_polygons(col = "outcome", style ="cont", palette = "-RdYlGn")Kilauea

© 2022 - 2024 — McMap. All rights reserved.