LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 25 lessons

Lesson 4 of 9

Module 3 · Figures that tell the truth

Colour that works for everyone

Colour that works for everyone

Colour that works for everyone

Among people of northern European ancestry, about 1 in 12 men and 1 in 200 women have a colour vision deficiency, most often red–green. Choosing the right kind of palette makes figures readable for them and clearer for everyone.

⏱ 12 min readModule 3 · Figures that tell the truthFree
Lesson at a glance6 steps · about 12 min
  1. Match the palette to the data
  2. How to read this table
  3. A diverging heatmap in R
  4. What this does, line by line
  5. Reading the output
  6. Why the rainbow fails

Match the palette to the data

A colour vision deficiency means that one of the three kinds of colour-sensing cone cells in the eye is missing or works differently. In the most common forms, the cones tuned to red or to green light are affected, so reds, greens, oranges and browns can look alike. People with these forms usually see blue and orange, and differences in lightness, without difficulty.

Colour vision deficiency is only one reason a figure's colours can fail. Figures are printed in greyscale, shown on washed-out projectors, viewed on phone screens and shrunk into small panels. A palette that works for everyone is one whose colours differ in lightness, how light or dark a colour looks, as well as in hue, which colour family it belongs to, such as red, blue or green. Lightness survives almost every one of those conditions; hue alone often does not. In Choosing the right chart you gave the two supplements a blue and an orange for this reason: they differ in hue for most readers and in lightness for everyone.

The first decision is not which colours you like, but what kind of data the colour has to carry. There are three kinds, and each needs its own type of palette (a planned set of colours).

The question to ask of any variable before you colour it is: does it have an order, and if so, does it have a meaningful middle? Treatment group or cell type has no order, so swapping two colours changes nothing. Gene expression has an order but no special middle: low is low and high is high. A log2 fold change has an order and a centre at 0, where nothing changed, and values on either side mean opposite things. Those three answers lead straight to the three rows of the table below.

DataPalette typeGood choices
Groups with no order (treatment, cell type)CategoricalOkabe–Ito: #E69F00 #56B4E9 #009E73 #F0E442 #0072B2 #D55E00 #CC79A7 #000000
Low to high (expression, counts)Sequentialviridis, cividis
Below and above a meaningful middle (fold change, z-score)Diverging, centred on the middleBlue–white–red (RdBu), purple–green (PRGn)

How to read this table

Find your data in the left column; the middle column names the palette type, and the right column gives tested examples.

  • A categorical palette uses clearly different hues of similar weight, so no group looks more important than another. The Okabe–Ito palette was designed so that its eight colours stay distinguishable for people with the common colour vision deficiencies. The codes are hex colours: a # followed by two hexadecimal digits each for red, green and blue, so #E69F00 is an orange and #0072B2 a blue. These two are the colours this module has used for orange juice and ascorbic acid.
  • A categorical palette has a practical limit. Beyond about six to eight groups, even well-chosen colours become hard to tell apart and to match with a legend. With more groups, combine rare ones into "other", label the groups directly on the plot, or give each its own panel. In R, palette.colors(palette = "Okabe-Ito") returns all eight Okabe–Ito colours without typing the codes, and scale_colour_manual(values = okabe) applies a named vector, as in Choosing the right chart.
  • A sequential palette runs from light to dark (or dark to light), so more always looks like more. viridis runs from dark purple through blue and green to yellow, and its lightness rises steadily, so it still reads correctly in greyscale. cividis is a blue-to-yellow variant designed to look almost the same to people with and without a red–green deficiency.
  • A diverging palette has two hues meeting at a neutral middle, usually white. It suits data with a natural centre, such as a log2 fold change of 0 (no change) or a z-score of 0 (exactly average). RdBu and PRGn are names from the ColorBrewer collection of palettes.

In ggplot2 each type has a ready-made scale. For categorical data, scale_colour_manual() with a palette you choose, or scale_colour_viridis_d(), the discrete version of viridis. For sequential data, scale_fill_viridis_c(), which also accepts option = "cividis". For diverging data, scale_fill_distiller(palette = "RdBu") or scale_fill_gradient2(), whose midpoint argument sets the value that receives the middle colour (by default 0). Whichever you use, check the legend to confirm which end of the scale is which colour: the common convention in expression figures is red for higher and blue for lower.

The usual mistake is a sequential palette on diverging data. Log2 fold changes drawn from white to dark red make a gene that halved look paler than one that stayed the same, as if it were merely "less". A diverging palette shows the decrease as a colour of its own.

The opposite mistake also happens: a diverging palette on data with no meaningful middle. If raw expression values are drawn from blue through white to red, the white falls at whatever value happens to sit halfway between the lowest and highest, and readers will take it as a natural boundary between "low" and "high" genes. Use a diverging palette only when you can name the middle value and say why it matters.

Colour that works for everyone · LSMA free course Top: a categorical row of distinct colours, a sequential ramp from light to dark, and a diverging ramp from navy through white to red. Bottom: the same diverging colours with limits minus 2 to 2, where white sits at 0, and with limits minus 1 to 3, where white sits at plus 1 and 0 takes a colour from the lower side. 1 · THREE KINDS OF PALETTE Categorical groups, no order Sequential low to high Diverging around a middle low high below middle above 2 · WHERE WHITE SITS MATTERS Limits −2 to 2 white at 0 Limits −1 to 3 white at +1 −2−1012 −10+123 0 (no change) now looks like a fall, and +1 looks unchanged.
The top three rows show what each palette type is for, drawn in this site's colours; for your own figures use the palettes named in the table. The bottom two bars use the same colours with different limits: moving the limits moves white away from zero. Schematic.

The Okabe–Ito palette comes from Masataka Okabe and Kei Ito's online guide Color Universal Design, and was brought to many biologists by Wong 2011, "Points of view: Color blindness" (Nature Methods). The case against the rainbow is made in Crameri, Shephard & Heron 2020, "The misuse of colour in science communication" (Nature Communications). cividis is described in Nuñez, Anderton & Renslow 2018 (PLOS ONE). Red–green colour vision deficiency affects roughly 1 in 12 men and 1 in 200 women of northern European ancestry; rates are lower in many other populations, so quote such figures with the population they describe.

A diverging heatmap in R

The code below draws a heatmap of the course file expression_example.csv with a blue–white–red palette. A heatmap is a grid in which each cell's colour shows a number: here, one row per gene and one column per sample.

In Figures with ggplot2 you drew a heatmap with pheatmap(mat, scale = "row"), which z-scores each gene for you inside the function. Here the z-scores are calculated explicitly first. That takes one extra line, but it lets you print their range and choose colour limits from the actual values, which is the step the worked example below depends on.

Files for this lessonSave each file in your project folder under the name shown here. Your browser may give the download a different name; rename it before you run the code.
expression_example.csvSimulated log2 expression: 30 genes, 8 samples
R
library(pheatmap)
expr <- read.csv("expression_example.csv", row.names = 1)
z <- t(scale(t(as.matrix(expr))))          # z-score each gene (row)
round(range(z), 2)

pheatmap(z[1:12, ], cluster_rows = FALSE, cluster_cols = FALSE,
         color = colorRampPalette(c("#2166AC", "white", "#B2182B"))(100),
         breaks = seq(-2, 2, length.out = 101))
# ggplot2: scale_colour_viridis_c(), scale_fill_distiller(palette = "RdBu", limits = c(-2, 2))

What this does, line by line

  • library(pheatmap) loads the pheatmap package, which draws heatmaps from a numeric matrix.
  • read.csv("expression_example.csv", row.names = 1) reads the file and uses its first column, the gene names, as row names rather than as data.
  • t(scale(t(as.matrix(expr)))) works from the inside out. as.matrix() turns the table into a numeric matrix. scale() converts each column to z-scores, so the matrix is first transposed with t() to make genes into columns, scaled, then transposed back. The result, z, has genes in rows again, each now a z-score.
  • A z-score is (value − the gene's mean) ÷ the gene's standard deviation. A z-score of 2 means "two standard deviations above this gene's own average". Every gene ends up on the same scale, whatever its raw level.
  • Why z-score at all? Genes differ enormously in their typical expression. Without scaling, a few highly expressed genes would take the darkest colours and every other gene would look flat. After scaling, each row shows how that gene varies across samples relative to its own average. The price is that the heatmap no longer shows which genes are highly or lowly expressed overall; a gene with tiny, noisy counts can look just as striking as a strongly expressed one. If overall level matters, draw it separately, for example with viridis on log-transformed values.
  • round(range(z), 2) prints the smallest and largest z-score in the matrix, to two decimal places.
  • pheatmap(z[1:12, ], ...) draws the first 12 genes. cluster_rows = FALSE and cluster_cols = FALSE keep the genes and samples in their original order instead of regrouping them.
  • colorRampPalette(c("#2166AC", "white", "#B2182B"))(100) builds 100 colours blending from a dark blue through white to a dark red.
  • breaks = seq(-2, 2, length.out = 101) sets 101 evenly spaced cut points from −2 to 2. They mark out 100 intervals, one for each colour, so the middle of the scale, white, falls exactly at 0.
  • The final comment names the ggplot2 equivalents: scale_colour_viridis_c() for a viridis colour scale on continuous data, and scale_fill_distiller(palette = "RdBu", limits = c(-2, 2)) for a ColorBrewer diverging fill with fixed limits.
[1] -2.01 2.00

Reading the output

The [1] marks the first element of the printed vector. The two numbers are the lowest and highest z-scores: −2.01 and 2.00. The range is almost exactly symmetric around 0, which is why limits of −2 and 2 suit these data.

Values that fall outside the limits need a decision. The lowest z-score, −2.01, lies just below −2. In ggplot2, values outside a scale's limits are treated as missing by default and drawn in a grey na.value colour, which a reader may mistake for real missing data. Adding oob = scales::squish to the scale gives those values the end colour instead, so −2.01 is drawn as dark blue, like −2. Squishing is honest if the legend makes clear that the end colours mean "this value or beyond". In pheatmap, check the same thing by comparing the printed range with your breaks, as here.

Three heatmaps of the same genes. The rainbow version shows false sharp boundaries. The blue-white-red version shows treated samples higher for the first six genes and lower for the next six. The viridis version shows overall expression level.
Rainbow (left) makes artificial bands and fails in greyscale; a diverging palette (middle) shows direction from the average; viridis (right) shows level. This comparison image was made with Python; the R code above draws the middle version.

Why the rainbow fails

The rainbow palette (called jet in some software) runs through blue, cyan, green, yellow and red. Its lightness does not rise steadily: yellow and cyan look much brighter than the colours around them. The eye reads those bright bands as edges in the data, so smooth changes appear as sharp boundaries that are not there. Printed in greyscale, low and high values can come out as similar greys, and its red and green ends are the pair most often confused by colour-blind readers.

Palettes such as viridis and cividis are called perceptually uniform: equal steps in the data look like roughly equal steps in colour, everywhere along the scale. That is the property the rainbow lacks. A related trap is the red–green heatmap, a convention from early microarray studies in which up-regulated genes were red and down-regulated ones green. It is a diverging palette built from exactly the two hues that red–green colour-blind readers confuse, so the two directions of change can look the same. Blue–white–red, or purple–green, keeps the idea and fixes the problem.

The same check works for any figure: print it in greyscale, or look at it with a colour-blindness simulator as described in the box below. If two groups or two directions become impossible to tell apart, change the palette, or add a second cue such as shape, line type or a direct label.

Worked example · centring a diverging scale

z-scores run from −2.01 to 2.00 → limits −2 to 2, with white at 0
if the limits were −1 and 3, white would sit at +1 and “no change” would look like an increase

Step by step. White is placed at the midpoint of the limits. For −2 and 2: (−2 + 2) ÷ 2 = 0. For −1 and 3: (−1 + 3) ÷ 2 = 1. In the second case a gene that rose by one standard deviation is coloured white and looks unchanged, while a gene at 0, which did not change at all, is coloured pale blue and looks as if it fell. The colour scale has shifted the reader's idea of "no change".

What the answer means. With diverging data, set limits that are symmetric around the meaningful middle, such as −2 and 2, even when the data are slightly lopsided. Compare the printed range with your limits, as the code does with range(), so you know whether any values, such as the −2.01 here, fall slightly outside them.

A second example of centring. Suppose a set of log2 fold changes runs from −1.5 to 4. Setting the limits to the data range would put white at (−1.5 + 4) ÷ 2 = 1.25, so a gene that doubled (log2 fold change 1) would look nearly unchanged. Symmetric limits of −4 and 4 keep white at 0; the blue side then uses only part of its range, which honestly shows that the decreases are smaller than the increases.

Go deeper: simulate colour blindness in R

You do not have to guess how a palette looks to a colour-blind reader. The colorspace package, on CRAN, converts colours to their appearance under the common deficiencies: deutan() for the green-cone type, protan() for the red-cone type and tritan() for the rare blue-cone type. desaturate() removes colour altogether, which mimics a greyscale printout.

Try library(colorspace); swatchplot(okabe, deutan(okabe), desaturate(okabe)) with the two-colour okabe vector from Choosing the right chart. Each row shows the same two colours as a different reader would see them. The blue and orange stay clearly different in every row, because they differ in lightness as well as hue: in greyscale the blue becomes a darker grey and the orange a lighter one. Run the same check on any palette before you commit a figure to it.

Key point

Never use colour alone to carry meaning. Add shapes, labels or direct annotation, and check your figure in greyscale or with a colour-blindness simulator.

A second example: on a survival curve comparing two treatments, drawing one line red and the other green relies entirely on colour. Using blue and orange helps, but writing the treatment name at the end of each line, or using a solid and a dashed line, makes the figure readable even in a black-and-white printout.

Further reading · Module 3Weissgerber et al. 2015: why bar charts hide the data, with examples. · All reading for this course

Check your understanding

1. Which palette suits log2 fold changes?

2. Why avoid the rainbow (jet) palette?

3. Which pair is hardest for the most common colour vision deficiency?

Up next

Lesson 5

Honest axes and scales

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.