Set the same colour range for 2 != rasters

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Set the same colour range for 2 != rasters

jacksonmrodrigues@gmail.com
Dear all,

My name is Jackson.
I am trying to set the same colour range for 2 rasters (max and min
temperatures). Both rasters have different numerical ranges but the same
dimensions
dimensions  : 4346, 4365, 18970290, 1  (nrow, ncol, ncell, nlayers)

The lowest value is 3 and the highest is 31. So my colour palette should
range from 3 to 31 and be useful for both temperatures

However I got a message saying that S4 and vector cannot be coerced.
So far I understand why it is not working but how to fix it?

A few lines from my code.

####
cols<-colorRampPalette(c("royalblue","springgreen","yellow","orange","red"))(29)

Temp.interval = seq(from=3, to=31)

# creating colour vectors
col1 <- cols[findInterval(TMin_masked$prj, vec =  Temp.interval  )]

Error in as.double(x) :
  cannot coerce type 'S4' to vector of type 'double'
####

Thank you all!

Best regards.

Jackson

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Set the same colour range for 2 != rasters

Sarah Goslee
Hi Jackson,

I think you would have gotten a faster response if you'd provided a
reproducible example. I at least let this message sit until I had time
to figure out what you were doing. If you have more raster questions,
there's also a r-sig-geo mailing list that would be more appropriate.

Nonetheless, you seem to be overthinking the problem. Is this what you need:

library(raster)

r1 <- matrix(sample(seq( 3, 20), size=25, replace=TRUE), 5, 5)
r2 <- matrix(sample(seq(15, 31), size=25, replace=TRUE), 5, 5)

r1 <- raster(r1)
r2 <- raster(r2)

cols <-colorRampPalette(c("royalblue","springgreen","yellow","orange","red"))(29)

# plot two rasters with different ranges but the same colors
par(mfrow=c(1, 2))
plot(r1, col=cols, zlim=c(3, 31))
plot(r2, col=cols, zlim=c(3, 31))

Sarah

On Sun, Sep 16, 2018 at 10:59 PM Jackson Rodrigues
<[hidden email]> wrote:

>
> Dear all,
>
> My name is Jackson.
> I am trying to set the same colour range for 2 rasters (max and min
> temperatures). Both rasters have different numerical ranges but the same
> dimensions
> dimensions  : 4346, 4365, 18970290, 1  (nrow, ncol, ncell, nlayers)
>
> The lowest value is 3 and the highest is 31. So my colour palette should
> range from 3 to 31 and be useful for both temperatures
>
> However I got a message saying that S4 and vector cannot be coerced.
> So far I understand why it is not working but how to fix it?
>
> A few lines from my code.
>
> ####
> cols<-colorRampPalette(c("royalblue","springgreen","yellow","orange","red"))(29)
>
> Temp.interval = seq(from=3, to=31)
>
> # creating colour vectors
> col1 <- cols[findInterval(TMin_masked$prj, vec =  Temp.interval  )]
>
> Error in as.double(x) :
>   cannot coerce type 'S4' to vector of type 'double'
> ####
>
> Thank you all!
>
> Best regards.
>
> Jackson
>

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.