View source: R/scale_palette.R
scale.palette | R Documentation |
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'.
scale.palette(ncols, cols, middle.col, span, middle, steps, invert=FALSE)
ncols |
The number of colors to be outputted. It is scaled linearly according to the given |
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 |
middle.col |
Optional. The color for the 'center' of the scale. It has to be one of the |
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 |
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 |
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 |
invert |
Optional. If |
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.
A vector that contains ncols
color codes.
# 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)])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.