Divergent color interpolation function with adjustable range and floor
Source:R/colorjam-gradient.R
col_div_xf.RdDivergent color interpolation function with adjustable range and optional color floor
Usage
col_div_xf(
x = 1,
floor = 0,
lens = 0,
n = 15,
colramp = "RdBu_r",
open_floor = FALSE,
debug = FALSE,
...
)Arguments
- x
numericvalue used as a threshold, where numeric values at or above this valuexare assigned the last color in the color gradient. Negative values at or below this negative value-xare assigned the first color in the color gradient.- floor
numericoptional value where numeric values between-xandxare assigned the middle color in the color gradient. Note that values at exactlyxor-xare assigned the next respective color away from the middle color. Whenfloor=0orfloor=NULLno floor is applied, and colors are assigned using a continuous range of numeric values from-xtoxwith lengthn.- lens
numericvalue indicating a color lens applied to the color gradient, passed tojamba::getColorRamp(). Lens valueslens > 0will condense the color gradient, making smaller changes more visually distinct;lens < 0expands the color gradient, making smaller changes less visually distinct.- n
integernumber of colors used for the initial color gradient. This value is forced to be an odd number, so the "middle color" will always be represented as one strict color. Note that when using afloor, the first non-middle color is used for thefloorassignment which means a smallernvalue will assign a more visibly distinct color than using a largern. See examples.- colramp
characterpassed tojamba::getColorRamp()which recognizes one of several forms of input:characterstring matching the name of a color ramp fromRColorBrewer(see divergent palettes withRColorBrewer::display.brewer.all(type="div")). Note that adding"_r"will reverse the color gradient, so the default"BuRd_r"will create a color gradient with "blue-white-red" - with red for high values consistent with "heat" in "heatmaps" - where heat is red.charactervector of R colors, which define a specific color ramp. This vector will be expanded tonlength.
- open_floor
logicalindicating whether colors below the assignedfloorwill still receive non-middle color. Settingopen_floor=TRUEis the best method to compare the effect of assigning the strict middle-color to values below thefloor, versus using gradient colors below thefloor, while all remaining numeric-color assignments are held constant.- debug
logicalindicating whether to produce a plot that shows the resulting color gradient.- ...
additional arguments are ignored.
Value
function that maps a vector of numeric values
to R colors using the divergent color gradient and numeric
thresholds defined.
Details
This function is intended to extend the very useful function
circlize::colorRamp2() which takes a numeric vector of
breaks, and a character vector of R colors, and returns
a function that maps numeric values to R colors using
interpolated color gradient. This function is intended for
specific cases using a divergent color gradient, where this
function assumes colors should be mapped to positive and
negative numeric values centered at zero.
A driving use case is with ComplexHeatmap::Heatmap(), with
argument col that contains a color function produced by
circlize::colorRamp2() or a color vector. However, when
supplying a divergent color vector, the colors are not applied
symmetrically above and below zero.
See also
Other colorjam gradients:
col_linear_xf(),
make_jam_divergent(),
twostep_gradient()
Other colorjam assignment:
col_linear_xf(),
group2colors(),
matrix2heatColors(),
rainbowJamMulti(),
vals2colorLevels(),
vibrant_color_by_hue()
Examples
col_fn1 <- col_div_xf(x=3, floor=0, n=21)
col_fn2 <- col_div_xf(x=3, floor=1, n=13)
col_fn3 <- col_div_xf(x=3, floor=1, n=9)
col_fn4 <- col_div_xf(x=3, floor=1, n=5)
col_fn2o <- col_div_xf(x=3, floor=1, n=13, open_floor=TRUE)
col_fn3o <- col_div_xf(x=3, floor=1, n=9, open_floor=TRUE)
col_fn4o <- col_div_xf(x=3, floor=1, n=5, open_floor=TRUE)
test_seq <- seq(from=-3, to=3, by=0.05);
names(test_seq) <- round(test_seq, digits=2);
opar <- par("mfrow"=c(1, 1));
bp0 <- barplot(abs(test_seq),
las=2, yaxt="n",
main="floor=0",
col=col_fn1(test_seq),
border="#22222222")
abline(v=bp0[abs(test_seq) == 1,], lty="dashed")
bp1 <- barplot(abs(test_seq),
las=2, yaxt="n",
main="floor=1",
col=col_fn2(test_seq),
border="#22222222")
abline(v=bp1[abs(test_seq) == 1,], lty="dashed")
bp2 <- barplot(abs(test_seq),
las=2, yaxt="n",
main="floor=1\nopen_floor=TRUE",
col=col_fn2o(test_seq),
border="#22222222")
abline(v=bp2[abs(test_seq) == 1,], lty="dashed")
par(opar)
test_seq <- seq(from=-3, to=3, by=0.5);
names(test_seq) <- round(test_seq, digits=2);
test_seq <- c(test_seq,
`-0.999`=-0.999,
`0.999`=0.999);
test_seq <- test_seq[order(test_seq)]
opar <- par("mfrow"=c(1, 2));
bp1 <- barplot((test_seq),
las=2, yaxt="n",
main="floor=1\nn=19",
col=col_fn2(test_seq),
border="#22222244")
abline(v=bp1[abs(test_seq) == 1,], lty="dashed")
bp2 <- barplot((test_seq),
las=2, yaxt="n",
main="floor=1\nn=19\nopen_floor=TRUE",
col=col_fn2o(test_seq),
border="#22222244")
abline(v=bp2[abs(test_seq) == 1,], lty="dashed")
bp3 <- barplot((test_seq),
las=2, yaxt="n",
main="floor=1\nn=9",
col=col_fn3(test_seq),
border="#22222244")
abline(v=bp3[abs(test_seq) == 1,], lty="dashed")
bp3 <- barplot((test_seq),
las=2, yaxt="n",
main="floor=1\nn=9\nopen_floor=TRUE",
col=col_fn3o(test_seq),
border="#22222244")
abline(v=bp3[abs(test_seq) == 1,], lty="dashed")
bp4 <- barplot((test_seq),
las=2, yaxt="n",
main="floor=1\nn=5",
col=col_fn4(test_seq),
border="#22222244")
abline(v=bp4[abs(test_seq) == 1,], lty="dashed")
bp4 <- barplot((test_seq),
las=2, yaxt="n",
main="floor=1\nn=5\nopen_floor=TRUE",
col=col_fn4o(test_seq),
border="#22222244")
abline(v=bp4[abs(test_seq) == 1,], lty="dashed")
par(opar)