细粒度的颜色渐变图例

时间:2015-04-29 09:32:57

标签: r gradient legend

制作颜色渐变图例时遇到问题。在我的情况下,数据的范围可以从0到1,并且值之间的差异可以非常小,使得图例看起来或多或少是单色的。有没有办法让渐变更精细?

#dummy data
prob <- c(0.24228,0.24330,0.19590,0.28554,0.18260,0.24626,0.21664,0.21598,0.22754,0.19366,0.36778,0.18854,0.48554,0.21922,0.20212,0.25406,0.18744,0.22476,0.17464,0.20828,0.19084,0.21878,0.17642,0.17190,0.31024,0.30492,0.16838,0.22374,0.20550,0.21550,0.19432,0.22890,0.17526,0.16864,0.33064,0.20454,0.19704,0.30926,0.18156,0.26320,0.18686,0.18234,0.17128,0.19186,0.39960,0.23060,0.26152,0.42696,0.25806,0.16712,0.17568,0.28280,0.19872,0.16790,0.28618,0.31684,0.36114,0.32234,0.23386,0.19820,0.21796,0.22628,0.16930,0.16700,0.16364,0.19562,0.19706,0.20404,0.22368,0.39426,0.23390,0.26944,0.22828,0.17430,0.16262,0.16722,0.16362,0.19150,0.39826,0.32130,0.28422,0.16902,0.19758,0.18310,0.17334,0.17308,0.16654,0.20186,0.26402,0.17904,0.20244,0.23508,0.25188,0.36882,0.18062,0.24956,0.20742,0.17328,0.18414,0.19626,0.19368,0.36542,0.23246,0.19910,0.31924,0.35536,0.31374,0.28556,0.29444,0.19620,0.18296,0.17322,0.33390,0.16920,0.30898,0.41456,0.16678,0.48374,0.24966,0.17130,0.42178,0.20878,0.22610,0.17204,0.19620,0.50638)

# update - fix
prob <- prob[!duplicated(prob)]

my.palette <- rev(rgb(prob[order(prob)], 1-prob[order(prob)], 0, maxColorValue=1))
image(1, prob[order(prob)], t(seq_along(prob)), col=my.palette, axes=FALSE, main="probability")
axis(2)

enter image description here

1 个答案:

答案 0 :(得分:2)

您似乎在绘制数据而不是制作传奇。

无论如何..问题是您的数据范围与maxColorValue不匹配。所以,你可以设置最大...

red <- prob[order(prob)]
green <- max(prob)-prob[order(prob)]
my.palette <- rev(rgb(red, green, 0, maxColorValue=max(prob)))
image(1, prob[order(prob)], t(seq_along(prob)), col=my.palette, axes=FALSE, 
      main="probability")
axis(2)

..或重新调整数据以获得更好的颜色变化。

scaledprob <- (prob[order(prob)]- min(prob)) / (max(prob) - min(prob))
my.palette <- rev(rgb(scaledprob, 1-scaledprob, 0, maxColorValue=1))
image(1, prob[order(prob)], t(scaledprob), col=my.palette, axes=FALSE, 
      main="probability")
axis(2)