In R, I have a raster object for which I would like to set a certain level of transparency. When I visualize it, everything is fine


test <- raster(x = matrix(1:16, 4))

     alpha = 0.25)

but now I would like to write my raster in .tif format through the writeRaster() function. Is it possible to store the level of transparency at the pixel level?

I need this step because, starting from the .tif object, I would like to produce tiles of such raster that will be displayed as basemaps Leaflet inside an online application.

1 Answer 1


Generally speaking a tiff (GeoTIFF) file is a means to store data. You are trying to store visualization metadata as a part of that file. While this might be possible, I would think twice before doing so: Usually visualization applications such as leaflet tiles allow you to set the transparency (opacity) value in the application itself (one example: Leaflet - Opacity method for L.tileLayer.wms?).

If you really want to create a transparency (alpha) band for the GeoTIFF file, you can do so with gdal after writing out the raster to GeoTIFF with R as explained in this thread: Adding transparency to Geotiff files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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