3

Say you want to plot a number of n different series in R (it doesn't really matter if you use plot(), ggplot2, or lattice. I will use ggplot2 for the sake of simplicity), how do you choose a beautiful set of colors (as in distinguishable, but good looking).

I know there are multiple approaches, but I wanted to get your opinion as well.

The Data:

library(ggplot2) # for easy plots

n <- 10 # n cases for different numbers

set.seed(42)
dat <- data.frame(id = "A",
              group = 1:n,
              val = rnorm(n))

1. GGplot standard:

advantage: scalable

disadvantage: not really beautiful in my opinion

ggplot(dat, aes(x = id, y = val, fill = as.factor(group))) + 
  geom_bar(stat = "identity", position = "dodge")

GGPlot standard

2. Rainbow:

Advantage: scalable, i.e., there is no maximum number of colors

ggplot(dat, aes(x = id, y = val, fill = as.factor(group))) + 
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = rainbow(n))

Rainbow

RColorBrewer

advantage: wide variety of different color sets

disadvantage: Only limited to a maximum of 12 colors

see ColorBrewer2 for all sets.

ggplot(dat, aes(x = id, y = val, fill = as.factor(group))) + 
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = brewer.pal(n, name = "BrBG")) # BrBG as an example

ColorBrewre BRBG

Question

How do you set the colors in your plot when you have potentially more than 10 cases? What criteria do you set when specifying the colors?

If you want to add factors that you feel are important, feel free to edit this post!

Edit:

Contrary to the charts shown, I deal with time-series, where the color of the series does not contain any information, except for the identification of the name of the series

David
  • 9,216
  • 4
  • 45
  • 78
  • 1
    Possible duplicate of [How to generate a number of most distinctive colors in R?](http://stackoverflow.com/questions/15282580/how-to-generate-a-number-of-most-distinctive-colors-in-r) – BenBarnes Oct 21 '15 at 10:36
  • 1
    You are limited by how well the human brain can distinguish colors. More than 10 will always be a problem. Look for a different approach to visualize your data. – Roland Oct 21 '15 at 10:53
  • 1
    maybe the viridis package on cran is useful. See [here](https://github.com/sjmgarnier/viridis) for the documentation. – phiver Oct 21 '15 at 10:53
  • 1
    RColorBrewer (both website and R package) are awesome for this! – rbatt Oct 21 '15 at 10:54
  • 2
    Honestly, my chosen color palettes are usually based on my organization's color palettes. As long as I can find strong, contrasting colors within the palette, I try to use those. But I try to avoid ever using more than four colors at a time unless I have a really good reason. Even four colors can make some graphs really difficult to interpret. – Benjamin Oct 21 '15 at 11:12
  • In my field (finance) asset returns are often visualised by a chart with multiple lines and colors. Therefore, the color is just for identification and does not contain any more information about values or something else... – David Oct 21 '15 at 11:54
  • 1
    Given that clarification, I second @phiver's viridis suggestion for this then (though I'm _really_ biased being a contributor to said pkg). Even with a crazy amt of lines (etc) you'll get distinct colors the majority of eyes can distinguish between. There are 4 variants for viridis as well in case blue/green/yellow aren't your cup of team. – hrbrmstr Oct 21 '15 at 12:13

2 Answers2

2

it may be not relevant, but I do nearly everything manually, using this site: http://tools.medialab.sciences-po.fr/iwanthue/

The theory tab explain well the methodology used.

YCR
  • 3,794
  • 3
  • 25
  • 29
2

I agree with Roland in that more than 10 is a problem.

I usually go with R defaults: black, red, green, blue, cyan, magenta, yellow, gray; then I may add an orange and a purple and a maroon...

But in your case, since you have bars, you can mix horizontal, diagonal and vertical lines, plus colors and widths, what gives you less headache about choosing so many colors.

Also: if your 10 factors are not linear, avoid colors that make a gradient, like your RColorBrewer example.

Also2: you can always go random until you find something you like, and then save it.

Rodrigo
  • 4,706
  • 6
  • 51
  • 94