Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommend a scale colour for 13 or more categories

Tags:

r

colors

ggplot2

The default scale for ggplot2 for 13 or more colours does not provide a high degree of visual differentiation. In addition, the longest of the brewer scales ends at 12 categories (Set3).

Can you recommend a colour scale that would be visually useful for 13 or more categories?

Reproducible example:

dat <- data.frame(value=rnorm(100),
category=sample(letters[1:13],100,replace=T),
other=sample(letters[1:5],100,replace=T))

# Default Scale
ggplot(dat, aes(other,value,color=category)) + 
geom_point(size=6) + 
coord_flip()

# Brewer Scale // notice the blank at the end!
ggplot(dat, aes(other,value,color=category)) + 
geom_point(size=6) + 
coord_flip() + 
scale_color_brewer(palette="Set3")

Note: facetting is not an option in my case (client doesn't like it, go figure)

like image 849
Brandon Bertelsen Avatar asked Nov 28 '12 23:11

Brandon Bertelsen


1 Answers

You could use colorRampPalette and scale_colour_manual to fudge a 13th category.

set3 <- colorRampPalette(brewer.pal('Set3',n=12))

ggplot(dat, aes(other,value,color=category)) + 
     geom_point(size=6) + 
     coord_flip() + 
     scale_color_manual(values = setNames(set3(13), levels(dat$category)))

This will break down, in that the colours will not be nicely distinguishable if you set the required numbers too high.

like image 98
mnel Avatar answered Oct 31 '22 01:10

mnel