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)

3
  • Can you provide a reproducible example ? Commented Nov 14, 2016 at 9: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. Commented Nov 14, 2016 at 9:42
  • 2
    A reproducible example always makes sense. Note how I create one in my answer. Commented Nov 14, 2016 at 9:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.