我有一组数据,并且想围绕一种“参考值”映射colour美学,如下所示:


以下值为红色
附近的值是蓝色的
上面的值是绿色


我仍然想证明值是连续的,所以我不想要简单地使用像cut()这样的函数并使用scale_colour_discrete。这是一些示例数据:

set.seed(123)
x <- runif(100, min = 1, max = 10)
y <- runif(100, min = 1, max = 10)
test <- data.frame(x = x, y = y)
test$colour <- runif(100, min = 1, max = 10)
ggplot(test, aes(x = x, y = y, colour = colour)) + geom_point(size = 3)


产生以下内容:



我对scale_colour_gradient(low = "red", high = "green")很熟悉,但是我希望能更加故意地沿所需的值映射过渡我的颜色,以使区域在视觉上更“流行”。间距不一定是线性的。换句话说,对于参考值为3,映射将如下所示:

value:  1     3      10
colour: red   blue   green


这可能吗?我还将采用其他解决方案来实现良好的可视化,以轻松地在众多的点中突出显示“所需”的值。例如,我考虑过替换引用附近的值,例如(ref - tol < colour < ref + tol) <- NA,然后对scale_colour_gradient使用na.value的选项。

还是我应该真正考虑使用cut()选项,只是使用很小的剪切尺寸,然后弄清楚如何随着结果中断逐渐改变颜色?

最佳答案

http://docs.ggplot2.org/0.9.2.1/scale_gradient2.html

scale_color_gradient2(low = "red", midpoint = 3, mid = "blue", high = "green")


更新:

关于OP的评论-玩中点和space="Lab"有助于:

# Using the Lab colour space also improves perceptual properties
# at the price of slightly slower operation
d + scale_colour_gradient2(space="Lab")


从墓地回来-根据最新的评论,我意识到应该将两行代码放在一起:

scale_color_gradient2(low = "red",
midpoint = 3,
mid = "blue",
high = "green",
space="Lab")

10-06 05:28
查看更多