R image function in R

R image function in R

You can define a bias in colorRampPalette. I have also adapted the function to define the number of steps between colors in color.palette:

#This is a wrapper function for colorRampPalette. It allows for the
#definition of the number of intermediate colors between the main colors.
#Using this option one can stretch out colors that should predominate
#the palette spectrum. Additional arguments of colorRampPalette can also
#be added regarding the type and bias of the subsequent interpolation.
color.palette <- function(steps, n.steps.between=NULL, ...){

 if(is.null(n.steps.between)) n.steps.between <- rep(0, (length(steps)-1))
 if(length(n.steps.between) != length(steps)-1) stop(Must have one less n.steps.between value than steps)

 fill.steps <- cumsum(rep(1, length(steps))+c(0,n.steps.between))
 RGB <- matrix(NA, nrow=3, ncol=fill.steps[length(fill.steps)])
 RGB[,fill.steps] <- col2rgb(steps)

 for(i in which(n.steps.between>0)){
  for(j in seq(3)){
   vals <- seq(col.start[j], col.end[j], length.out=n.steps.between[i]+2)[2:(2+n.steps.between[i]-1)]  
   RGB[j,(fill.steps[i]+1):(fill.steps[i+1]-1)] <- vals

     new.steps <- rgb(RGB[1,], RGB[2,], RGB[3,], maxColorValue = 255)
 pal <- colorRampPalette(new.steps, ...)

Heres an example of both (I have squeezed the number of steps between cyan and yellow):

Z <- t(as.matrix(1:100))

pal.1 <- colorRampPalette(c(blue, cyan, yellow, red), bias=1)
pal.2 <- colorRampPalette(c(blue, cyan, yellow, red), bias=3)
pal.3 <- color.palette(c(blue, cyan, yellow, red), n.steps.between=c(10,1,10))

image(Z, col=pal.1(100))
image(Z, col=pal.2(100))
image(Z, col=pal.3(100))


Also, if your interested, I wrote a function that plots a color scale and uses the same arguments as image. If you set up your plot layout correctly, this would also be a fast way to plot your matrices and corresponding color scale.

R image function in R

Leave a Reply

Your email address will not be published.