Skip to contents

Create custom color scale with enhanced visibility for small values

Usage

create_custom_colorer(
  palette = "RdYlBu",
  zero_color = "#FFFFFF",
  n_colors = 11,
  gradient_scaling = "enhanced",
  enhanced_threshold_pct = 50,
  enhanced_expansion_factor = 1.5
)

Arguments

palette

Name of diverging palette from colorspace package (default: "RdYlBu"). Other options include "RdBu", "PiYG", "BrBG", "PuOr", "RdGy".

zero_color

Color for zero values in hex (default: "#FFFFFF" - white for clear distinction)

n_colors

Number of color steps (default: 11)

gradient_scaling

Type of gradient scaling: "linear" or "enhanced" (default: "enhanced"). "linear" uses standard color mapping. "enhanced" gives more color distinction to smaller values.

enhanced_threshold_pct

Percentage of maximum value to use as threshold for enhanced scaling (default: 50). Values below this percentage get more color distinction.

enhanced_expansion_factor

Factor controlling how much more color distinction small values get (default: 1.5). Higher values mean more distinction for small differences. Only used when gradient_scaling = "enhanced".

Value

A colorer function that can be passed to plot_tg() or create_tg_animation()

Details

The enhanced scaling works by compressing small values in the scaled space, which gives them more colors in the final gradient. For example, if your data ranges from 0-20 percent and you use enhanced_threshold_pct = 50 with enhanced_expansion_factor = 2.0, then values 0-10 percent will get twice as much color distinction compared to linear scaling.

Examples

# Create dummy data for testing
dummy_data <- data.frame(
  Deliberate.Constraints = sample(1:6, 100, replace = TRUE),
  Automatic.Constraints = sample(1:6, 100, replace = TRUE)
)

# Basic usage with linear scaling
colorer <- create_custom_colorer(gradient_scaling = "linear")

# Enhanced scaling for better small value distinction
colorer_enhanced <- create_custom_colorer(
  gradient_scaling = "enhanced",
  enhanced_threshold_pct = 30,
  enhanced_expansion_factor = 2.0
)