0

I'm trying to get the colours using tmap::tm_raster to be the same as when plotted using raster::plotRGB.

my data set is here https://drive.google.com/drive/folders/1a5gOJ2S6lf_5nWu2ViU-6TRfi_gTMqi-?usp=sharing

First I upload my data:

library(raster)

DEM_grey <- raster::raster("DEM100GREY_clip.tif")

Then I plot it with tmap

library(tmap)

tm_map::tm_shape(DEM_grey)+
  tmap::tm_raster(palette = "-Greys")

and it looks like this enter image description here

Now I plot it with raster

library(raster)

raster::plotRGB(DEM_grey)

and I get the grey scale I want enter image description here

any thoughts on how I get the tmap_raster to look like the plotRGB image using tmaps?

Thanks, Simon

Simon
  • 295
  • 1
  • 4
  • 12

1 Answers1

0

I came with one solution that might not be the most elegant, but it obtains a very similar result as the one you posted. As tm_raster categorizes the raster values into discrete classes, you can assign the breaks manually to show, for example, your DEM in 255 different tones of grey.

library(tmap)
library(raster)

#Get the image min, max and the range divided by 255 
#255 is the number of different tones of grey you will get in your plot
rast_min<-cellStats(DEM_grey,min)
rast_max<-cellStats(DEM_grey,max)
rast_step<-ceiling((rast_max-rast_min)/255)

tm_shape(DEM_grey)+
  tm_raster(palette = "-Greys",
            breaks = seq(rast_min,rast_max,rast_step),
            #You can hide the legend using legend.show = F
            legend.show = F)