2

I am trying to replicate a plot that I have made in raster using the new(ish) terra R package but the alpha argument in terra::plot does not seem to work the same way as it does in raster.

I am trying to use the alpha layer to indicate uncertainty in the base (coloured) layer. Below is some simplified code indicating my problem. Using the raster package, the alpha layer changes the transparency based on values of each individual pixel. When using terra this doesn't seem to work.

I think I am using the latest version of terra (1.3.4), and I am on a Mac (version 15.5.11).

library(terra)
library(raster)

par(mfrow=c(1,2))

plot_cols<-terrain.colors(n=5)

## raster package
raster.template<-raster(xmn=1, xmx=5, ymn=1, ymx=5, nrows=5, ncols=5)

legend.ras.comm<-raster.template
legend.ras.comm[]<-rep(c(5,4,3,2,1), 5)

legend.ras.alpha<-raster.template
legend.ras.alpha[]<-rep(c(0.9,0.7,0.45,0.2,0), 5)

plot(t(legend.ras.comm), col=plot_cols, legend=FALSE, axes=FALSE, box=FALSE, main="Raster package")
plot(t(legend.ras.comm), col="#000000", alpha=legend.ras.alpha, add=TRUE, legend=FALSE)


## terra package
raster.template<-rast(xmin=1, xmax=5, ymin=1, ymax=5, nrows=5, ncols=5)

legend.ras.comm<-raster.template
legend.ras.comm[]<-rep(c(5,4,3,2,1), 5)

legend.ras.alpha<-raster.template
legend.ras.alpha[]<-rep(c(0.9,0.7,0.45,0.2,0), 5)

plot(t(legend.ras.comm), col=plot_cols, legend=FALSE, axes=FALSE, main="Terra package")
plot(t(legend.ras.comm), col="#000000", alpha=legend.ras.alpha, add=TRUE, legend=FALSE, axes=FALSE)

Plot output

Any help with this would be appreciated.

1 Answer 1

3

You can now do this with terra 1.3-22

I get, using your code:

## raster package
library(raster)
legend.ras.comm <- raster(xmn=1, xmx=5, ymn=1, ymx=5, nrows=5, ncols=5, vals=rep(c(5,4,3,2,1), 5))
legend.ras.alpha <- setValues(raster.template, rep(c(0.9,0.7,0.45,0.2,0), 5))
legend.ras.comm <- t(legend.ras.comm)
## terra package
library(terra)
legend.ter.comm <- rast(legend.ras.comm)
legend.ter.alpha <- rast(legend.ras.alpha)

par(mfrow=c(1,2))
plot_cols <- terrain.colors(n=5)

plot(legend.ras.comm, col=plot_cols, legend=FALSE, axes=FALSE, box=FALSE, main="Raster package")
plot(legend.ras.comm, col="#000000", alpha=legend.ras.alpha, add=TRUE, legend=FALSE)

plot(legend.ter.comm, col=plot_cols, legend=FALSE, axes=FALSE, main="Terra package")
plot(legend.ter.comm, col="#000000", alpha=legend.ter.alpha, add=TRUE, legend=FALSE, axes=FALSE)

enter image description here

With terra you can also use a single value that affects all colors

plot(legend.ter.comm, col=plot_cols, alpha=.75)

Or change the transparency by color. Something like this:

plot_cols_alpha <- terrain.colors(n=5, alpha=seq(.75, 1, .05))
plot(legend.ter.comm, col=plot_cols_alpha)
2
  • Thank you for your help. That solved my problem, though I did have to use a workaround. In terra, plotting the alpha layer as above doesn't work if there are NAs (i.e. areas of ocean if you're plotting on land, etc.). I managed to get it to work in terra by reclassifying NA as 1 the alpha mask, making them fully transparent. For a reproducible example, put the following two lines of code after the layers are created using raster: legend.ras.comm <- extend(x=legend.ras.comm, y=c(1,1)) legend.ras.alpha <- extend(x=legend.ras.alpha, y=c(1,1)) Aug 24, 2021 at 3:28
  • The problem with NAs is a bug that has been solved in the development version. You can install it with: install.packages('terra', repos='https://rspatial.r-universe.dev') Aug 24, 2021 at 9:01

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.