Show R heatmap outliers in a different colour

前端 未结 2 820
星月不相逢
星月不相逢 2021-01-16 11:37

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

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-16 12:14

    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.

提交回复
热议问题