Boris Leroy
Boris Leroy

Reputation: 500

How to specify a single colour for rasters with tmap?

I would like to adjust the basic colour of a raster plotted with tmap, when there is only one value in the raster.

Here is a very simple reproducible example:

library(raster)
library(tmap)
a <- raster(matrix(sample(c(1, NA), 10000, replace = TRUE, prob = c(0.01, 0.99)), nr = 100, nc = 100, ))

tm_shape(a) +
  tm_raster()

Basic raster plot

You can see that the default yellow colour is barely visible by a human eye. Hence, when drawing a map where you only have a few pixels, it is extremely difficult to locate where are the pixels with values.

Unfortunately, I was not able to change this colour after multiple attempts. I think this issue may be encountered by other users, thus if a simple answer arises here it might be very helpful.

Unsuccessful attempts:

tm_shape(a) +
  tm_raster(col = "black")

fail 1

tm_shape(a) +
  tm_raster(palette = "RdBu")

Note: for this one, I expected either a Red or a Blue colour to show up. Not grey... I tried to adjust midpoints as well but nothing changed.

fail 2

tm_shape(a) +
  tm_raster() +
  tm_layout(aes.color= c(fill = "black"))

enter image description here

Upvotes: 3

Views: 1872

Answers (1)

Humpelstielzchen
Humpelstielzchen

Reputation: 6441

Apparently, when you just specifiy col= it colors the whole raster in one color. So I guess you have to chose the layer where the points are on? And then provide a argument to palette= as explained in the documentation.

This is how I got it to work:

tm_shape(a) +
  tm_raster(col = "layer", palette = "black") 

enter image description here

Upvotes: 3

Related Questions