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.

1 Answers1

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")
missuse
  • 19,056
  • 3
  • 25
  • 47
  • 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")` – user3386170 Mar 03 '20 at 14:28