2

Working with ggplot and shiny, and plotting a lot of data to generate some interactive plots.

I have some performance problems, so I've checked with benchplot() my plotting time, and some of the big plot's are slow. For example, this is the time that it took me to plot one of those plots-

       step user.self sys.self elapsed
1 construct     0.093    0.005   0.101
2     build     1.528    0.044   1.583
3    render     3.292    0.070   3.446
4      draw     3.102    0.189   3.521
5     TOTAL     8.015    0.308   8.651

I can't plot with ggvis or ggbio, because they don't have faceting, which is essential.

Is there a way to cache the constructing, building and rendering of the plot, so I only need to draw it asked, and can save half of the time?

(saving pictures is not a possibility, because the plot are interactive)

timat
  • 1,480
  • 13
  • 17
T.G.
  • 743
  • 3
  • 6
  • 27
  • Can you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610) ? – timat Nov 14 '16 at 09:29
  • I thought about it, but it really doesn't make sense. My question is not about any specific data, It's about is there any way to cache the plot progress in the middle of it. – T.G. Nov 14 '16 at 09:42
  • 2
    A reproducible example always makes sense. Note how I create one in my answer. – Roland Nov 14 '16 at 09:50

1 Answers1

4

Yes, there is:

p <- ggplot(iris, (aes(x = Species, y = Sepal.Length))) +
  geom_boxplot()

g <- ggplotGrob(p)
library(grid)
grid.newpage()
grid.draw(g)


system.time(print(p))
#user  system elapsed 
#0.11    0.00    0.11 


system.time({
  grid.newpage()
  grid.draw(g)
  })
#user  system elapsed 
#0.03    0.00    0.03 

But also consider if you create the right kind of plot. E.g., if you plot hundreds of thousands of points, you are creating a plot that contains huge amounts of overplotting.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Thanks! What your suggesting is a great start, it saves the constructing time. Is there a way to do the same for building/rendering? (that take much more time) I'm not sure if it's possible, and I don't even understand what is each step, tried to find some info, but no success. – T.G. Nov 14 '16 at 10:03
  • No, I don't think this is possible in the grid graphics system. If these steps take so much time see the last remark in my answer. – Roland Nov 14 '16 at 10:12
  • Thanks! I saw what you wrote, and I don't think I'm using the wrong plot, but it did made me think of a way to separate it into 2 different plots, each one much smaller.. trying it now. – T.G. Nov 14 '16 at 10:48