1

I have this data:

r <-raster(volcano)

And the plot generated:

plot(r)

enter image description here

How to generate or export a raster with this color palette (or any other) with spatial reference?A RGB/stack raster. For instance, in QGIS we can export a raster as a rendered image after defined the renderization.

enter image description here

  • Do you want the axes and legend included? Or just the raster itself? – Spacedman May 22 at 6:20
  • Just the raster itself – Artur_Indio May 22 at 13:01
1

The palr package has a function image_raster for the task of baking in the palette to RGB version of the raster. (You can set the extent and projection using raster itself as usual).

library(raster)
#> Loading required package: sp
r <-raster(volcano)

library(palr)

## apply the colours used by raster plot() itself
rgb0 <- image_raster(r, col = rev(terrain.colors(255)))
plotRGB(rgb0)

Created on 2020-05-22 by the reprex package (v0.3.0)

There are arguments to control the col, breaks, and zlim after graphics::image().

Use raster::writeRaster() to create a GeoTIFF (or other format).

(To create PNG or JPEG I would convert the raster to array and use png or jpeg packages, just fwiw)

| improve this answer | |
  • Good alternative this package. – Artur_Indio May 22 at 17:23
  • One question. If I want to put contours like contour(volcano) is there any work around? – Artur_Indio May 22 at 21:10
  • it's surprisingly hard to do that actually, you need to set up the device itself to be the same resolution as the data, then draw to that, add the contours, and then capture it to file (the device might be a file, but only png or tiff). So then you have to add the georef. It's on my list to "make easy", "illustrate" etc - one of those obvious things that the tools work against you unfort (I'll have a think about the easiest way) – mdsumner May 23 at 2:59
  • I catch the basic idea. Who knows a possible update to this package ou an ideia to a new package transform any raster device to RGB – Artur_Indio May 23 at 16:17
  • I just discover that RGB() function from raster package makes the same that image_raster() – Artur_Indio May 23 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.