使用R / ggplot2控制中间值颜色与scale_colour_continuous [英] Control middle value colours with scale_colour_continuous using R/ggplot2
问题描述
我有一组数据,并希望将颜色
映射到某种参考值,如下所示:
- 以下的值为红色
- 接近的值为蓝色
- >
我还想表明这些值是连续的,所以只需使用 cut / code>并使用
scale_colour_discrete
不是我要找的。下面是一些示例数据:
set.seed(123)
x < - runif(100,min = max = 10)
y< - runif(100,min = 1,max = 10)
test< - data.frame(x = x,y = y)
test $ color < runif(100,min = 1,max = 10)
ggplot(test,aes(x = x,y = y,color = color))+ geom_point(size = 3)
这会产生以下结果:
我熟悉 scale_colour_gradient(low =red,high =green)
,但我希望更有意识地沿着期望的值映射转换我的颜色,使得区域更流畅。间距不一定是线性的。换句话说,对于参考值3,映射将是这样:
值:1 3 10
color:red blue green
这是否可能?我也会采取替代的解决方案,以完成一个良好的可视化,突出理想值很容易在一点点。例如,我考虑替换参考附近的值,使(ref-tol
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)
UPDATE: / p>
关于OP的评论 - 玩中点和 space =Lab
有助于:
#使用Lab颜色空间也可以提高知觉属性
#以稍慢的操作价格
d + scale_colour_gradient2(space =Lab )
I have a set of data and would like to map the colour
aesthetic around a sort of "reference value" like so:
- values below are red
- values near are blue
- values above are green
I still want to show the fact that values are on a continuoum, so simply using a function like cut()
and using scale_colour_discrete
is not what I'm looking for. Here's some example data:
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)
That produces the following:
I'm familiar with scale_colour_gradient(low = "red", high = "green")
, however I was hoping to more deliberately transition my colours along desired value mapping to make the regions "pop" a bit more visually. Spacing would not necessarily be linear. in other words, for a reference value of 3, the mapping would be something like this:
value: 1 3 10
colour: red blue green
Is this possible? I'd also take alternative solutions to accomplish a good visualization to highlight "desirable" values easily among a sea of points. For example, I considered replacing values near the reference such that (ref - tol < colour < ref + tol) <- NA
, and then using scale_colour_gradient
's option for na.value
.
Or should I truly be considering the cut()
option, just with a very small cut size and then figure out how to gradually change the colours along the resultant breaks?
From http://docs.ggplot2.org/0.9.2.1/scale_gradient2.html:
scale_color_gradient2(low = "red", midpoint = 3, mid = "blue", high = "green")
UPDATE:
Concerning OP's comment - playing around with midpoints and space="Lab"
helps:
# Using the Lab colour space also improves perceptual properties
# at the price of slightly slower operation
d + scale_colour_gradient2(space="Lab")
这篇关于使用R / ggplot2控制中间值颜色与scale_colour_continuous的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!