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)