10 November 2021

packages

library(ggplot2)
library(dichromat)
## palettes etc.
library(viridis)
library(cividis) ## https://github.com/marcosci/cividis
library(colorspace)
library(RColorBrewer)
library(cowplot)
library(colorBlindness)

use of colour

  • qualitative (factors): want distinctive colours
    • probably can’t get more than about 8 really distinctive colours
    • less if you need to be color-blind friendly
    • sometimes it helps to combine with shape
      (redundant coding)
  • quantitative (continuous values)
    • sequential or diverging
    • consider using background color for zero point
    • or else clearly partition color values from background

colour specification in R

  • via names:
head(colours())
## [1] "white"         "aliceblue"     "antiquewhite"  "antiquewhite1"
## [5] "antiquewhite2" "antiquewhite3"
  • via hex specification: #RRGGBB (additive, hexadecimal, colour specification: e.g. #FFFFFF = white, #FF0000 = red (sometimes a fourth hex value indicates alpha-transparency)
  • adjustcolor function can help you fiddle a color that you already like
  • apropos("colo|rgb"), ignore.case = FALSE) brings up a whole bunch of relevant stuff (the pipe means “or” here)

fade via transparency gradient

dd <- data.frame(x=1:9)
ggplot(dd,aes(x=x,y=1,alpha=x)) +
    geom_point(size=10,colour="red") +
    coord_fixed(ratio=5)+theme_void()

fade via colour gradient

ggplot(dd,aes(x=x,y=1))+
  geom_point(size=10,aes(colour=x))+
  scale_colour_gradient(low="lightgray",high="red")+
  coord_fixed(ratio=5)+theme_void()

saturation

  • want large blocks of colour to be unsaturated
  • small areas usually need to be saturated

display.brewer.all()

colour spaces

  • want perceptually uniform spaces (Zeileis, Hornik, and Murrell 2009)
  • RGB: additive, pixel-based
  • HSV (hue, saturation, value): unbalanced
  • HCL (hue, chroma, luminance): balanced (Zeileis et al. 2019)

Zeileis, Hornik, and Murrell (2009) Fig 1

Old Faithful data (duration vs waiting time)

HSV, grayscale, HSV heat, HCL heat

Zeileis, Hornik, and Murrell (2009) Fig 3

(left) hue varies, constant saturation & value; (right) hue varies, constant chroma & luminance

Zeileis et al. (2019) Fig 7

CIELAB space

Zeileis, Hornik, and Murrell (2009) Fig 4

Zeileis, Hornik, and Murrell (2009) Fig 7

colorspace palettes

png("pix/colorspace.png")
hcl_palettes(plot=TRUE)
dev.off()

colorspace palettes

colorspace demos

png("pix/colordemo.png")
par(mfrow = c(3, 3), mar = c(1,1,1,1))
cl <- sequential_hcl(5, "Heat")
for (i in c("map", "heatmap", "scatter", "spine",
            "bar", "pie", "perspective", "mosaic", "lines")) {
  demoplot(cl, type = i)
}
dev.off()

colorspace demos

I Want Hue

Why stick to a single path in the colour space? Why not fill an area?

demo

hues, randomcoloR (randomColor() !)

colourblind-friendliness

  • most common: deuteranomaly, \(\approx\) 5% of males
  • red-green axis is collapsed in various ways
  • trivia: “Daltonism” (after John Dalton)

viridis vs. cividis

illustrate dichromat package

par(mfrow=c(2,2),mar=c(1,1,1,1))
p <- brewer.pal("Set1",n=6)
pie(rep(1,6), col=p,main="ColourBrewer Set1")
pie(rep(1,6), col = dichromat(p))
p2 <- brewer.pal("Dark2",n=6)
pie(rep(1,6), col=p2, main="ColourBrewer Dark2")
pie(rep(1,6), col = dichromat(p2))

more packages

  • colorblindr (clauswilke/colorblindr), colorBlindness (CRAN)
colorBlindness::displayAvailablePalette(color="white")

online

contrast illusions

Colour appearance depends on neighbours

(Wikipedia)

  • think about backgrounds!
  • another reason to distrust heatmaps

cultural expectations

  • red-yellow-green is still used a lot …
  • political parties
  • “topographic” colours

(ggplot(dd,aes(x=x,y=1,colour=factor(x)))
  + geom_point(size=10)
  +  coord_fixed(ratio=5)
  + theme(legend.position="none")
  +   scale_colour_manual(values=topo.colors(9))
  + theme_void()
  + theme(legend.position="none")
)

more on available stuff …

library(ggplot2); library(colorspace); library(viridis)
length(apropos("scale_colour_"))
## [1] 36

references

Zeileis, Achim, Jason C. Fisher, Kurt Hornik, Ross Ihaka, Claire D. McWhite, Paul Murrell, Reto Stauffer, and Claus O. Wilke. 2019. “Colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes.” arXiv:1903.06490, March. http://arxiv.org/abs/1903.06490.

Zeileis, Achim, Kurt Hornik, and Paul Murrell. 2009. “Escaping RGBland: Selecting Colors for Statistical Graphics.” Computational Statistics & Data Analysis 53 (9): 3259–70. https://doi.org/10.1016/j.csda.2008.11.033.