Skip to contents

Turns colour stops and optional limits into the colorscale, cmin and cmax of a plotly coloraxis, or of a trace once cmin and cmax are renamed to its zmin and zmax. The stops are spaced evenly from min to mid and from mid to max, so a middle stop sits exactly at mid and a midpoint off centre stretches one side of the scale rather than moving its ends.

Usage

resolve_colorscale(
  colors = NULL,
  min = NA,
  mid = NA,
  max = NA,
  reverse = FALSE,
  data.range = c(0, 1)
)

Arguments

colors

Character vector of colours, lowest value first. A named vector, as a multiColorPicker() reports, is taken in order. NULL uses the first preset of uniform_colorscale_inputs_ui().

min, mid, max

Numeric limits and midpoint. NA or NULL (a cleared numeric input) falls back to the ends of data.range and the centre of the limits. A mid outside the limits is held just inside them.

reverse

Logical; run the colours from high to low.

data.range

The values plotted, or their range, used for a blank limit. Non-finite values are ignored. With none left, the limits are 0 and 1.

Value

A list with colorscale (a list of list(position, colour) stops running from 0 to 1), cmin, and cmax. cmin is always below cmax: limits given in the wrong order are swapped, and equal limits (constant data) are widened by 0.5 either way.

Details

These are the values of the inputs from uniform_colorscale_inputs_ui(). A scale set on layout.coloraxis rather than on the trace can be changed without redrawing the plot: it is a layout-only update, as a styling step should be.

Author

Jared Andrews

Examples

# Betas, with the middle colour at 0.3.
scale <- resolve_colorscale(c("blue", "white", "red"), min = 0, mid = 0.3, max = 1)
scale$colorscale
#> [[1]]
#> [[1]][[1]]
#> [1] 0
#> 
#> [[1]][[2]]
#> [1] "blue"
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 0.3
#> 
#> [[2]][[2]]
#> [1] "white"
#> 
#> 
#> [[3]]
#> [[3]][[1]]
#> [1] 1
#> 
#> [[3]][[2]]
#> [1] "red"
#> 
#> 

fig <- plotly::plot_ly(z = volcano, type = "heatmap", coloraxis = "coloraxis")
plotly::layout(fig, coloraxis = scale)