我使用image
命令绘制矩阵,矩阵包含一些NA
值。我正在使用image
喜欢
image(figData, zlim = zRange, col = colors, useRaster=TRUE)
zRange
小于figData
的实际范围。我正在使用以下组中的顶部色彩图,这与红绿色感知缺陷兼容:
http://www.mathworks.com/matlabcentral/fx_files/31761/6/thumbnail_184481.jpg
我想为NA
中figData
以外的colors
中的gray
值选择特定颜色(例如,NA
)。 colors
值现在已映射到white
(na.color
)的最后一个元素,因此我无法将它们与超出范围的值区分开来。
理想情况下,无论我使用哪种调色板,我都希望有一种解决方案。
我想要heatmap.2
中与image
选项类似的内容,我该怎么做?我愿意修改image
源代码来完成此操作,但是rasterImage
似乎调用了image.nan <- function(z, zlim, col, na.color='gray', outside.below.color='black', outside.above.color='white',...)
{
zstep <- (zlim[2] - zlim[1]) / length(col); # step in the color palette
newz.below.outside <- zlim[1] - zstep # new z for values below zlim
newz.above.outside <- zlim[2] + zstep # new z for values above zlim
newz.na <- zlim[2] + 2 * zstep # new z for NA
z[which(z<zlim[1])] <- newz.below.outside # we affect newz.below.outside
z[which(z>zlim[2])] <- newz.above.outside # we affect newz.above.outside
z[which(is.na(z>zlim[2]))] <- newz.na # same for newz.na
zlim[1] <- zlim[1] - zstep # extend lower limit to include below value
zlim[2] <- zlim[2] + 2 * zstep # extend top limit to include the two new values above and na
col <- c(outside.below.color, col, outside.above.color, na.color) # we construct the new color range by including: na.color and na.outside
image(z=z, zlim=zlim, col=col, ...) # we finally call image(...)
}
函数,其源代码我找不到。
在接受了Jealie的回答之后,我在这里包含了代码,以便对低于和高于该范围的值进行不同的考虑:
{{1}}
答案 0 :(得分:2)
小校正:外部的值未映射到col
的最后一个元素,但它们根本没有被绘制(并且没有颜色看起来是白色的。)。
否则,要回答您的问题,最简单的方法是在image
周围编写一个包装器,其中包含两个新参数:na.color
和outside.color
。以下是我的建议,对于NA值,默认为gray
;对于white
以外的值,默认为zlim
:
my.image <- function(figData, zlim, col, na.color='gray', outside.color='white', ...)
{
newz.na <- zlim[2]+(zlim[2]-zlim[1])/length(col) # new z for NA
newz.outside <- zlim[2]+2*(zlim[2]-zlim[1])/length(col) # new z for values outside zlim
figData$z[which(is.na(figData$z>zlim[2]))] <- newz.na # we affect newz.outside
figData$z[which(figData$z<zlim[1] | figData$z>zlim[2])] <- newz.outside # same for newz.na
zlim[2] <- zlim[2]+2*(zlim[2]-zlim[1])/length(col) # we finally extend the z limits to include the two new values
col <- c(col, na.color, outside.color) # we construct the new color range by including: na.color and outside.color
image(figData, zlim=zlim, col=col, ...) # we finally call image(...)
}
答案 1 :(得分:2)
因为颜色间隔定义为(*bottom*, *top*]
,所以z中等于zlim [1]的值将使用outside.below.color着色(因为outside.below.color的范围是{{1 }})。
以下版本修复了此错误:
( *zlim[1] - zstep* , *zlim[1]* ]
比较
image.nan.better <- function(z, zlim, col, na.color='gray', outside.below.color='black', outside.above.color='white',...)
{
zstep <- (zlim[2] - zlim[1]) / length(col); # step in the color palette
newz.below.outside <- zlim[1] - 2 * zstep # new z for values below zlim
newz.above.outside <- zlim[2] + zstep # new z for values above zlim
newz.na <- zlim[2] + 2 * zstep # new z for NA
z[which(z<zlim[1])] <- newz.below.outside # we affect newz.below.outside
z[which(z>zlim[2])] <- newz.above.outside # we affect newz.above.outside
z[which(is.na(z>zlim[2]))] <- newz.na # same for newz.na
zlim[1] <- zlim[1] - 2 * zstep # extend lower limit to include below value
zlim[2] <- zlim[2] + 2 * zstep # extend top limit to include the two new values above and na
col <- c(outside.below.color, col[1], col, outside.above.color, na.color) #correct by including col[1] at bottom of range
image(z=z, zlim=zlim, col=col, ...) # we finally call image(...)
}