Recommend a scale colour for 13 or more categories

2020-02-17 10:42发布

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)

标签: r colors ggplot2
1条回答
不美不萌又怎样
2楼-- · 2020-02-17 11:06

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.

查看更多
登录 后发表回答