scale.palette: Function to provide a user-defined scaled color palette

View source: R/scale_palette.R

scale.paletteR Documentation

Function to provide a user-defined scaled color palette

Description

This function gives a vector of colors to use following given input colors, with a range following a given span, and with an adjustable 'center' of the scale (i.e., the palette can be asymmetric). This is particularly useful for color gradients that follow continuous, numeric, vectors: the user can thus adjust which color is in the 'center' of the scale, and what are the 'color paths' on each side of that 'center'.

Usage

scale.palette(ncols, cols, middle.col, span, middle, steps, invert=FALSE)

Arguments

ncols

The number of colors to be outputted. It is scaled linearly according to the given span.

cols

Optional. The colors to be used. At least two colors are expected, three if the user wants to give a 'middle'. Without color given, the assumed vector is c("white","black").

middle.col

Optional. The color for the 'center' of the scale. It has to be one of the cols elements. If not, it is computed as being the middle color between the two most extreme given colors, weighted by the middle of the span if they are provided. Type middle.col=NA if no middle color is desired but is acknowledged, avoiding warning messages.

span

Optional. A numeric vector of two values that are the extremities of the range of the values used. For instance, it can be the minimal and maximal values of a continuous numeric vector the colors are intended to follow. If not provided, it is set to c(0,1). Type span=NA if no span is desired but is acknowledged, avoiding warning messages.

middle

Optional. A numeric value that is the 'center' of the color range. This is particularly useful if one wants an asymmetrical range of colors. If not provided, it is the half of the span. Type middle=NA if a (default) halfway middle is desired but is acknowledged, avoiding warning messages.

steps

Optional. A vector giving the steps between colors if one wants a multi-step scaled palette. Must be the length of the length of cols minus two (i.e., the two extreme colors).

invert

Optional. If invert = TRUE, it flips the color ramp; by default set to FALSE.

Details

The function uses the function colorRampPalette in order to provide a continuous color gradient following a given input of colors and with a given length. This function enables the user to get an asymmetrical palette, either in the color repartition or in the extreme colors of the palette.

Value

A vector that contains ncols color codes.

Examples

# For a vector going from 0 to 100, with colors from red to blue:
plot(1:100,1:100,pch=21,col=NA,bg=scale.palette(ncols=100,cols=c("red","blue"),span=c(0,100)))

# The same example but with a 'middle color' at the 20, thus skewing the color ramp with more reds between 0 and 20 and more blues between 20 and 100:
plot(1:100,1:100,pch=21,col=NA,bg=scale.palette(ncols=100,cols=c("red","blue"),span=c(0,100),middle=20))

# For a vector going from 0 to 100, with colors from red to blue to green:
plot(1:100,1:100,pch=21,col=NA,bg=scale.palette(ncols=100,cols=c("red","green3","blue"),middle.col="green3",span=c(0,100)))

# The same example but with a 'middle color' at the 20, thus skewing the color ramp with reds to blues between 0 and 20 and blues to greens between 20 and 100:
plot(1:100,1:100,pch=21,col=NA,bg=scale.palette(ncols=100,cols=c("red","green3","blue"),middle.col="green3",span=c(0,100),middle=20))

# For a vector going from -50 to 100, with blues towards -50, reds towards 100, and a white center at 0:
plot(-50:100,-50:100,pch=21,col=NA,bg=scale.palette(ncols=151,cols=c("blue","white","red"),middle.col="white",span=c(-50,100),middle=0))

# For a vector following an already defined-gradient, and by skewing its center at 0.75 (i.e., with more reds at the middle):
require(RColorBrewer)
user_color_gradient<-brewer.pal(8,"YlOrRd") # To have a regular "heat" gradient from yellow to orange to red
plot(1:100,1:100,pch=21,col=NA,bg=scale.palette(ncols=100,cols=user_color_gradient,middle.col=user_color_gradient[6],span=c(0,100)))
# Or with by skewing the center at 0.25 (i.e., with more yellows):
plot(1:100,1:100,pch=21,col=NA,bg=scale.palette(ncols=100,cols=user_color_gradient,middle.col=user_color_gradient[2],span=c(0,100)))

# For a multi-steps gradient

require(ULT)
ncols<-round(runif(1,1000,10000))
cols<-contrasting.palette(1:round(runif(1,2,18)))
span<-c(-13,17.5)
steps<-seq(span[1],span[2],length.out=(length(cols)))[-c(1,length(cols))]

bg_cols<-scale.palette(ncols=ncols,cols=cols,middle.col=NA,span=span,middle=NA,steps=steps,invert=FALSE)
plot(seq(span[1],span[2],length.out=ncols),seq(span[1],span[2],length.out=ncols),pch=21,col=NA,bg=bg_cols)
abline(v=span[1],lwd=2,col=cols[1])
for (i in 1:length(steps)){
  abline(v=steps[i],lwd=2,col=cols[i+1])
}
abline(v=span[2],lwd=2,col=cols[length(cols)])


jacobmaugoust/ULT documentation built on May 16, 2023, 1:29 p.m.