Learn R Programming

oce (version 0.8-10)

rescale: Rescale values to lie in a given range

Description

Rescale values to lie in a given range

Usage

rescale(x, xlow, xhigh, rlow=0, rhigh=1, clip=TRUE)

Arguments

x
a numeric vector.
xlow
x value to correspond to rlow. If not given, it will be calculated as the minimum value of x
xhigh
x value to correspond to rhigh. If not given, it will be calculated as the maximum value of x
rlow
value of the result corresponding to x equal to xlow.
rhigh
value of the result corresponding to x equal to xhigh.
clip
logical, set to TRUE to clip the result to the range spanned by rlow and rhigh.

Value

  • A new vector, which has minimum lim[1] and maximum lim[2].

Details

This is helpful in e.g. developing a color scale for an image plot. It is not necessary that rlow be less than rhigh, and in fact reversing them is a good way to get a reversed colorscale for a plot.

Examples

Run this code
low <- 0
high <- 1
x <- runif(1000, min=low, max=high)
y <- runif(1000, min=low, max=high)
z <- sqrt(0.5 * (x^2+y^2))
# "jet" color scheme
drawPalette(zlim=c(low,high), col=oceColorsJet)
col <- oceColorsJet(256)[rescale(z, xlow=low, xhigh=high, rlow=1, rhigh=256)]
plot(x, y, col=col, cex=4, pch=20, asp=1)
contour <- interpBarnes(x,y,z)
contour(contour$xg, contour$yg, contour$zg, add=TRUE, labcex=1, levels=seq(0,1,0.2))

# forward and reverse "jet" colorscale
i <- 1:100
plot(i, rep(0, 100), pch=20, cex=2, col=oceColorsJet(100)[rescale(i, rlow=1, rhigh=100)])
points(i, rep(1, 100), pch=20, cex=2, col=oceColorsJet(100)[rescale(i, rlow=100, rhigh=1)])

Run the code above in your browser using DataLab