Search code examples
rimagecolorbar

Adjust color scale of image plot in R


The image.plot function automatically generate the color scale for the inputs. For example,

library(fields)
x <- 1:10
y <- 1:15
z <- outer(x, y, "+")
image.plot(x, y, z)

will generate a figure like this enter image description here Now I want to adjust the color scale to the range from 0 to 20, and any values above 20 need to be colored the same as 20. So I added zlim=c(0,20) in the function parameters, and I get, enter image description here The problem is, although zlim adjusted the color scale range, the values above the limit were not assigned a color. How can I fix this? In this case, I want the up-right corner to be colored dark red i.e. to have the color assigned from the end of the color range.


Solution

  • You could use z[z > 20] <- 20 to achieve that with image.plot.

    library(fields)
    x <- 1:10
    y <- 1:15
    z <- outer(x, y, "+")
    z[z > 20] <- 20
    image.plot(x, y, z)
    

    enter image description here