当所有元素的值相同时,热映射颜色

时间:2018-06-21 19:26:03

标签: r heatmap heatmaply

我正在使用heatmaply生成二进制矩阵的“热图”,其中矩阵中的每个值都是1或0。我将1设置为红色,将0设置为灰色,并且工作正常。代码如下:

library(tidyverse)
library(heatmaply)
dummy_data <- replicate(10, ifelse(rnorm(20)>0,1,0))
uniform_data <- replicate(10, replicate(20,1))
heatmaply(
        dummy_data,
        dendrogram = 'none',
        Rowv = FALSE,
        Colv = FALSE,
        col = c("#C0C0C0","red"),
        hide_colorbar = TRUE,
        file='./heatmap-dummy.html',
        grid_gap = 1)
heatmaply(
        uniform_data,
        dendrogram = 'none',
        Rowv = FALSE,
        Colv = FALSE,
        col = c("#C0C0C0","red"),
        hide_colorbar = TRUE,
        file='./heatmap-uniform.html',
        grid_gap = 1)

这在第一种情况下工作正常(其中dummy_data同时包含1和0)。但是,uniform_data只有1,并且代码生成所有灰度的热图。

我希望热图全都是红色。我该如何实现?我尝试将颜色更改为c("red", "#C0C0C0")c("red","red")c("red"),但这没有任何效果。

我的会话信息:

R> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin17.5.0 (64-bit)
Running under: macOS High Sierra 10.13.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib

locale:
[1] en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
 [1] heatmaply_0.15.0   viridis_0.5.1      viridisLite_0.3.0  plotly_4.7.1.9000
 [5] forcats_0.3.0      stringr_1.3.1      dplyr_0.7.5        purrr_0.2.5
 [9] readr_1.1.1        tidyr_0.8.1        tibble_1.4.2       ggplot2_2.2.1.9000
[13] tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] nlme_3.1-137       bitops_1.0-6       lubridate_1.7.4    webshot_0.5.0
 [5] RColorBrewer_1.1-2 httr_1.3.1         prabclus_2.2-6     tools_3.5.0
 [9] R6_2.2.2           KernSmooth_2.23-15 lazyeval_0.2.1     colorspace_1.3-2
[13] trimcluster_0.1-2  nnet_7.3-12        withr_2.1.2        tidyselect_0.2.4
[17] gridExtra_2.3      mnormt_1.5-5       compiler_3.5.0     cli_1.0.0
[21] rvest_0.3.2        Cairo_1.5-9        TSP_1.1-6          xml2_1.2.0
[25] diptest_0.75-7     caTools_1.17.1     scales_0.5.0       DEoptimR_1.0-8
[29] mvtnorm_1.0-8      psych_1.8.4        robustbase_0.93-0  digest_0.6.15
[33] foreign_0.8-70     pkgconfig_2.0.1    htmltools_0.3.6    htmlwidgets_1.2
[37] rlang_0.2.1        readxl_1.1.0       rstudioapi_0.7     shiny_1.1.0
[41] bindr_0.1.1        jsonlite_1.5       crosstalk_1.0.0    mclust_5.4
[45] gtools_3.5.0       dendextend_1.8.0   magrittr_1.5       modeltools_0.2-21
[49] Rcpp_0.12.17       munsell_0.5.0      yaml_2.1.19        stringi_1.2.3
[53] whisker_0.3-2      MASS_7.3-50        flexmix_2.3-14     gplots_3.0.1
[57] plyr_1.8.4         grid_3.5.0         promises_1.0.1     parallel_3.5.0
[61] gdata_2.18.0       crayon_1.3.4       lattice_0.20-35    haven_1.1.1
[65] hms_0.4.2          pillar_1.2.3       fpc_2.1-11         reshape2_1.4.3
[69] codetools_0.2-15   stats4_3.5.0       glue_1.2.0         gclus_1.3.1
[73] data.table_1.11.4  modelr_0.1.2       httpuv_1.4.3       foreach_1.4.4
[77] cellranger_1.1.0   gtable_0.2.0       kernlab_0.9-26     assertthat_0.2.0
[81] mime_0.5           xtable_1.8-2       broom_0.4.4        later_0.7.3
[85] class_7.3-14       seriation_1.2-3    iterators_1.0.9    registry_0.5
[89] bindrcpp_0.2.2     cluster_2.0.7-1

1 个答案:

答案 0 :(得分:2)

这似乎是密谋的错误。请参阅以下代码(非常粗略地)表示heatmaply中发生的事情:

library(ggplot2)
x <- rep(1, 10)
g <- ggplot(data.frame(x), aes(x=x, y=x, fill=x)) + 
    geom_tile() + 
    scale_fill_viridis(limits = c(0, 1))
g

ggplot2 heatmap with correct colour scale

ggplotly(g)

plotly heatmap with incorrect colour scale