Show R heatmap outliers in a different colour

a 夏天 提交于 2019-12-19 11:42:05

问题


Looking to mark outliers in R matrix in a different color. Say I have the data as

1  2  4  2  5
5  4  3  2  3
1 500 5  4  2

Now I want to mark the 500 with a different color and the remaining of the matrix with default color of the heatmap.

Can someone guide me through the process?


回答1:


If you want to clearly see the 500, you should specify no scaling. For example,

m <- matrix(c(1, 5, 1, 2, 4, 500, 4, 3, 5, 2, 2, 4, 5, 3, 2), 
  ncol=5)

heatmap((m<500)+0, scale="none", Rowv=NA, Colv=NA)



回答2:


Here's a decent workaround to achieve this. Using heatmap.2() from gplots, you can specify any colour for NA values in your heatmap. So, if you use a simple function to replace outliers with NAs in the source matrix, you can then represent them with any colour you like.

First, choose your outlier condition. For example's sake, let's just say that any value greater than 10 is an outlier.

> m
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    2    4    2    5
# [2,]    5    4    3    2    3
# [3,]    1  500    5    4    2

m[m > 10] <- NA

Now plot the heatmap.

library(plots)
heatmap.2(m, trace = "none", na.color = "Green")

Outlier is now nice and obvious.



来源:https://stackoverflow.com/questions/39292369/show-r-heatmap-outliers-in-a-different-colour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!