How can I make a heatmap with a large matrix?

前端 未结 6 1535
Happy的楠姐
Happy的楠姐 2020-12-14 03:56

I have a 1000*1000 matrix (which only includes integer 0 and 1), but when I tried to make a heatmap, an error occurs because it is too large.

How can I create a heat

6条回答
  •  不知归路
    2020-12-14 04:19

    Using heatmap3, which is more memory efficient than the default heatmap function and faster through it's use of the fastcluster package to do the hierarchical clustering works fine for me. Adding argument useRaster=TRUE also helps :

    library(heatmap3)
    nrowcol <- 1000
    dat <- matrix(ifelse(runif(nrowcol*nrowcol) > 0.5, 1, 0), nrow=nrowcol)
    heatmap3(dat,useRaster=TRUE)
    

    The useRaster=TRUE seems quite important to keep memory use within limits. You can use the same argument in heatmap.2. Calculating the distance matrix for the hierarchical clustering is the main overhead in the calculation, but heatmap3 uses the more efficient fastcluster package for that for large matrices. With very large matrices you will unavoidably get into trouble though trying to do a distance-based hierarchical cluster. In that case you can still use arguments Rowv=NA and Colv=NA to suppress the row and column dendrograms and use some other logic to sort your rows and columns, e.g.

    nrowcol <- 5000
    dat <- matrix(ifelse(runif(nrowcol*nrowcol) > 0.5, 1, 0), nrow=nrowcol)
    heatmap3(dat,useRaster=TRUE,Rowv=NA,Colv=NA)
    

    still runs without problems on my laptop with 8 Gb memory, whereas with the dendrograms included it already starts to crunch.

提交回复
热议问题