16

We are using R to spit out plots(heatmaps) which are being rendered on a shiny app (web page). Currently we are facing an issue with the time it takes R to render a plot taking out the time it takes to do the computation. Let me show the same through a contrived example. In this basic test case R takes ~17 seconds to render and save a heatmap file as png (data computer time is taken out : row and cols clusters are precomputed)

I am wondering is there a way to reduce the time it takes to render this plot type by a significant factor. Maybe I am missing on some other constant computation which can be also taken out of the heatmap function.

Thanks!

generate data

m1 <- matrix(rnorm(500000,mean=15,sd=4),ncol=100)
m2 <- matrix(rnorm(500000,mean=30,sd=3),ncol=100)
m <- cbind(m1,m2)
dim(m)

basic heat map with all computation

png('test_heatmap.png')
system.time(heatmap(m))

user  system elapsed 
29.327   0.637  30.526 

do the clustering out of heatmap function : mainly to test the plot rendering time

> system.time(hcr <- hclust(dist(m)))
   user  system elapsed 
  9.992   0.126  10.144 
> system.time(hcc <- hclust(dist(t(m))))
   user  system elapsed 
  0.659   0.002   0.662 
> system.time(ddr <- as.dendrogram(hcr))
   user  system elapsed 
  0.498   0.010   0.508 
> system.time(ddc <- as.dendrogram(hcc))
   user  system elapsed 
  0.011   0.000   0.011 

heatmap rendering time with pre-computed row/col dendogram

png('test_heatmap.png')
> system.time(heatmap(m,Rowv=ddr,Colv=ddc))
   user  system elapsed 
 16.128   0.558  17.171 
igauravsehrawat
  • 3,696
  • 3
  • 33
  • 46
Abhi
  • 6,075
  • 10
  • 41
  • 55
  • 8
    Try to set `useRaster=TRUE` in `heatmap`. The parameter is documented in `?image`. – Roland Nov 22 '13 at 16:08
  • @Roland : thnks for quick reply. Where exactly should I be using this option.. Sorry quick google dint help me much.. – Abhi Nov 22 '13 at 16:10
  • `heatmap(m,Rowv=ddr,Colv=ddc,useRaster=TRUE)` – Roland Nov 22 '13 at 16:12
  • thanks... I found this to be helpful too. http://blog.revolutionanalytics.com/2011/07/paul-murrell-on-incorporating-images-in-r-charts.html – Abhi Nov 22 '13 at 16:15
  • 1
    The raster package might also be of interest. – Roland Nov 22 '13 at 16:21
  • aaha great. I would invest sometime to look into it..hopefully rendering heatmap through it wont be non-trivial..thanks – Abhi Nov 22 '13 at 16:24
  • my answer to http://stackoverflow.com/questions/5667107/r-how-can-i-make-a-heatmap-with-a-large-matrix?rq=1 showed a 10fold speedup from using `useRaster=TRUE` – Ben Bolker Nov 22 '13 at 17:05
  • ... although when I tried just now I didn't get that much of an advantage. I think that rendering the dendrograms is taking a long time ... – Ben Bolker Nov 22 '13 at 17:14
  • 2
    Javascript rendering ? http://joecheng.com/R/heatmap.html ; discussion: https://groups.google.com/d/topic/shiny-discuss/rJhy5sUOe1U/discussion) – Stéphane Laurent Nov 22 '13 at 17:58
  • If you are happy with raster package would you consider posting an answer to your own question to clean up the unanswered stack and guide further searches? – cmbarbu Feb 25 '15 at 23:05

2 Answers2

2

geom_raster( ), from the ggplot2 package, provides high performance tiling. It may accelerate the heatmap visualization, once the clustering has been performed.

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
leo9r
  • 2,037
  • 26
  • 30
0

Another thing to consider is:

library(lattice)
levelplot(hclust(dist(m)))
rcorty
  • 1,140
  • 1
  • 10
  • 28