Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make R render plots faster

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 
like image 793
Abhi Avatar asked Nov 22 '13 15:11

Abhi


People also ask

Is Ggplot better than base R?

ggplot2 vs Base R Base R plots two vectors as x and y axes and allows modifications to that representation of data whereas ggplot2 derives graphics directly from the dataset. This allows faster fine-tuning of visualizations of data rather than representations of data stitched together in the Base R package1.

What does GG plot do in R?

ggplot2 is a plotting package that provides helpful commands to create complex plots from data in a data frame. It provides a more programmatic interface for specifying what variables to plot, how they are displayed, and general visual properties.


1 Answers

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

like image 143
leo9r Avatar answered Sep 24 '22 21:09

leo9r