R ggplot2 - 如何指定越界值的颜色 [英] R ggplot2 - How do I specify out of bounds values' colour

查看:885
本文介绍了R ggplot2 - 如何指定越界值的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码生成的图中,我想改变颜色,以便所有值< 0.6与低颜色相同,而大于1的所有值都是高颜色。

正如现状一样,颜色渐变横跨整个数字范围的数据。我尝试添加限制,但是这使得所有超出界限的值与NA值相同,这不是我想要的,因为我需要缺少NA值以清楚地伸出并且看起来不像超出界限值<0.6 。



我相信,答案是oob,打破了论点,但没有成功实现它。

  library(ggplot2)
a = rnorm(17 * 17,0.733,0.21)
qcMat = matrix(a,ncol = 17)
qcMat [qcMat> 1] = 1
#qcMat包含0和1之间的值,并且一些NAs

m = melt(t(qcMat))
m $ Var2 < - 其中(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 =))


解决方案

正如你所说,你需要 oob 参数在 scale_fill_gradient 中。要限制值,您可以使用从音阶包裹中挤压(安装 ggplot2 时安装 scales ):

  library(比例)

及更高版本

  scale_fill_gradient(low =red,high =green,limits = c(0.6,1),oob = squish)


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.

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.

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 = " "))

解决方案

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)

and later

scale_fill_gradient(low = "red", high = "green", limits=c(0.6, 1), oob=squish)

这篇关于R ggplot2 - 如何指定越界值的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆