随机化ggplot中大型集的定性颜色

时间:2014-01-25 15:50:08

标签: r ggplot2

如果你要定性地映射到大量组的颜色,ggplot的自动颜色分配会相邻地绘制非常相似的颜色,这使得很难看出哪个引用哪个键等。为了说明:

require(ggplot2); require(stringr)
df = data.frame(x = letters, y = sample(20:100,26), lab=word("apple ball cat dog elephant frog goat hat ice jackal king lion mango nest owl parrot queen rabbit ship tomato umbrella van watch xylophone yatch zebra", 1:26))
p = ggplot(df, aes(x, y, fill=lab)) + geom_bar(stat="identity")
p + scale_fill_discrete()

enter image description here

可以手动混合一些随机颜色:

cols = rainbow(26, s=.6, v=.9)[sample(1:26,26)]
p + scale_fill_manual(values=cols)

enter image description here

..导致更有用的彩虹分裂,但这看起来很笨拙,仍然留下一些颜色聚集在一起,一般不理想。 ggplot是否有一个本机方法来实现这样的目标(但希望更好)?

1 个答案:

答案 0 :(得分:13)

为这么多颜色制作一个好的调色板确实是一项艰巨的任务。但是,有一种解决方案可能会有所帮助。前段时间我分叉了this repo并找到了对iWantHue的引用。据我所见,生成的调色板已经混合,因此相邻的颜色看起来可以区分。

例如,我有你的例子 enter image description here

以防万一,调色板是

"#89C5DA", "#DA5724", "#74D944", "#CE50CA", "#3F4921", "#C0717C", "#CBD588", "#5F7FC7", 
"#673770", "#D3D93E", "#38333E", "#508578", "#D7C1B1", "#689030", "#AD6F3B", "#CD9BCD", 
"#D14285", "#6DDE88", "#652926", "#7FDCC0", "#C84248", "#8569D5", "#5E738F", "#D1A33D", 
"#8A7C64", "#599861"