简体   繁体   English

R ggplot2-如何指定边界值的颜色

[英]R ggplot2 - How do I specify out of bounds values' colour

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. 在下面的代码生成的图中,我想更改颜色,以使所有<0.6的值都与“低”颜色相同,而所有大于1的值都为“高”颜色。

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. 我尝试添加限制,但是这会使所有超出范围的值与NA值具有相同的颜色,这不是我想要的,因为我需要缺少NA值才能清楚地突出显示,并且看起来与<0.6的超出范围的颜色不一样。

I'm convinced that the answer is with the oob, breaks arguments but have had no success getting it to work. 我坚信答案是oob,打破了争论,但未能成功。

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 . 正如您自己说的那样,您需要在scale_fill_gradient使用oob参数。 To clamp values, you can use squish from the scales package ( scales is installed when ggplot2 is installed): 为了钳位值,你可以使用嘎吱从鳞包scales当安装ggplot2安装):

library(scales)

and later 然后

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

Try changing geom_tile to below: 尝试将geom_tile更改为以下内容:

  geom_tile(aes(fill = ifelse(value<0.6,min(m$value,na.rm=TRUE),
                              ifelse(value>1,max(m$value,na.rm=TRUE),
                                     value))))

EDIT: Then don't use min max, so we will get 0.6-1 range: 编辑:然后不要使用最小最大,所以我们将得到0.6-1范围:

geom_tile(aes(fill = ifelse(value<0.6,0.6,
                            ifelse(value>1,1,
                                   value))))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ggplot2、R 中指定所有因子相同的颜色和透明度? - How to specify all factors the same colour and transparency in ggplot2, R? 使用r和ggplot如何指定特定的十六进制颜色/颜色值来表示不同的字符值 - Using r and ggplot How do I specify specific hexadecimal color/colour values to use to represent different character values 如何在ggplot2的scale_colour_gradientn中指定颜色编号 - How to specify the colour number in scale_colour_gradientn of ggplot2 如何在ggplot2中使用颜色条来指示不同的类别? - How do I colour bars in ggplot2 to indicate different categories? 如何将三角形添加到 R 中的 ggplot2 颜色条以指示超出范围的值? - How can I add triangles to a ggplot2 colorbar in R to indicate out of bound values? 如何为连续数据指定ggplot2箱线图填充颜色? - How to specify ggplot2 boxplot fill colour for continuous data? 绘图时,如何过滤掉 ggplot2 中总和 &lt; 10 的值? - When plotting, how do I filter out values that have a sum of < 10 in ggplot2? How do I specify a fixed number of colors to appear in a color bar that labels binned data in an R plot generated with ggplot2? - How do I specify a fixed number of colors to appear in a color bar that labels binned data in an R plot generated with ggplot2? 如何在 ggplot2 中将 Y 轴上的标签隔开? - How Do I Space Out the Labels on the Y axis in ggplot2? 我如何为 ggplot2 分布同一年的温度? - How do I spread out temperatures in the same year for ggplot2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM