rspatial/terra

Display color raster with plet()

Closed this issue · 12 comments

Given

s <- rast(system.file("ex/logo.tif", package="terra"))   
s
plot(s)

image

Is it possible to get plet() to display the same colors?

plet(s)

image

Same result with

ss <- colorize(s, "col")
plet(ss)

With terra 1.7.83 I get now:

s <- rast(system.file("ex/logo.tif", package="terra"))  
plet(s)

image

which is better but still not the same as
plot(s)

Thanks. This now works as expected.

Yes!
But:

s <- rast(system.file("ex/logo.tif", package="terra"))/100
plotRGB(s,1,2,3)
plotRGB(s,1,2,3,stretch="lin")
plet(s)

image

The stretch="lin" option for plet() would be most useful.

You can now do

library(terra)
s <- rast(system.file("ex/logo.tif", package="terra"))
plet(s, stretch="hist")
image

Not with last version 1.8.3:


> library(terra)
terra 1.8.3
s <- rast(system.file("ex/logo.tif", package="terra"))/100
plotRGB(s,1,2,3)
plotRGB(s,1,2,3,stretch="lin")

image

plet(s)
image

Same with:

plet(s, stretch="hist")
plet(s, stretch="lin")

By my example works (at least for me). You divide the raster by 100 and then it is no longer an RGB image.

You divide the raster by 100 and then it is no longer an RGB image.

stretch="lin" is not supposed to take care of that, as it does in plotRGB()?

With plotRGB there is no doubt that we are dealing with an RGB raster. With plot and plet that needs to be explicit.

For example compare the two plots

library(terra)
s <- rast(system.file("ex/logo.tif", package="terra"))
plot(s)
plot(s/100)

I see, but

> class(s)
[1] "SpatRaster"
attr(,"package")
[1] "terra"
> class(s/100)
[1] "SpatRaster"
attr(,"package")
[1] "terra"

still think that stretch="lin" should transform the object to the (0,255) range, and thus I would expect
plot(r/100, stretch="lin)
to be the same colors as
plot(r, stretch="lin")

But you must have your reasons.
What I would like to know is, how should we display r/100 to have the same colors as plot(r) ?
Just plotRGB(r/100, stretch="lin) ?

What about plet()? there is no pleRGB()

A SpatRaster looses its RGB channels if you do an arithmetic operation

library(terra)
s <- rast(system.file("ex/logo.tif", package="terra"))
RGB(s)
RGB(s/100)

You can set the channels when needed

r <- s/100
RGB(r) <- 1:3
# or set.RGB(r, 1:3)
plot(r, stretch="lin")

To make that more compact, you can now also do

r <- s/100
plot(RGB(r, 1:3), stretch="lin")

There is no pletRGB because my thinking is that you can accomplish the same with the above at the expense of a pair of parentheses. In stead of pletRGB() you now have plet(RGB())

Excellent!