问题描述
在由以下代码生成的图中,我想更改颜色,以便所有值
In the plot generated by the following code I would like to alter the colours so that all values < 0.6 are the same as the "low" colour and all values greater than 1 are the "high" colour.
按原样,颜色渐变横跨数据的整个数字范围.我已经尝试添加限制,但这使得所有越界值与 NA 值的颜色相同,这不是我想要的,因为我需要缺少 NA 值才能清楚地突出并且看起来与越界值的颜色不同
As is stands the colour gradient stretches across the entire numeric range of the data. I have tried adding limits but that makes all out of bounds value the same colour as NA values, which is not what I want because I need missing NA values to clearly stick out and not look the same colour as out of bounds values <0.6.
我确信答案是 oob,它打破了争论,但没有成功让它发挥作用.
I'm convinced that the answer is with the oob, breaks arguments but have had no success getting it to work.
library(ggplot2)
a = rnorm(17*17, 0.733,0.21)
qcMat = matrix(a, ncol = 17)
qcMat[qcMat> 1] = 1
#qcMat contains values between 0 and 1 and some NAs
m = melt(t(qcMat))
m$Var2 <- with(m,factor(Var2, levels = rev(sort(unique(Var2)))))
ggplot(m, aes(as.factor(Var1), Var2, group=Var2)) +
geom_tile(aes(fill = value)) +
geom_text(aes(fill = m$value, label = round(m$value, 2))) +
scale_fill_gradient(low = "red", high = "green") +
xlab("") + ylab("") + ggtitle(paste("biscuit:", biscuit_id, "- QC", sep = " "))
推荐答案
正如您自己所说,您需要 scale_fill_gradient
中的 oob
参数.要限制值,您可以使用 squish 来自 scales 包(scales
在安装 ggplot2
时安装):
As you said youself, you want the oob
argument in the scale_fill_gradient
. To clamp values, you can use squish from the scales package (scales
is installed when ggplot2
is installed):
library(scales)
以后
scale_fill_gradient(low = "red", high = "green", limits=c(0.6, 1), oob=squish)
这篇关于R ggplot2 - 如何指定越界值的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!