Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In short, yes, there are ways to speed up the printing of a lattice plot based on pre-computed grid objects or other parameters. However, as far as I know, there is no simple solution. To do so, I was required to delve into the source of the lattice plotting function <code>plot.trellis</code> to identify spots where I could optimize speed. Continuing from the example in the question...</p> <pre><code># Save the legend from the last plot plot.grob = trellis.last.object() legend.grob = lattice:::evaluate.legend( plot.grob$legend ) # Modify the default plotting function f = lattice:::plot.trellis b = body(f) fun.line = which(!is.na(str_match(as.character(b),"evaluate.legend"))) new.line = substitute(legend &lt;- if(is.null(legend.object)) evaluate.legend(x$legend) else legend.object) body(f)[[fun.line]] = new.line args = formals(f) formals(f) = do.call(alist, c( args[2:length(args)-1], alist(legend.object=NULL,...=)) ) # Note that evaluate.legend is no longer taking up any time because it's not called Rprof() f(plot.grob,legend.object=legend.grob) Rprof(NULL) head(summaryRprof()$by.total) # the modified function is faster times = data.frame( modified.fun=NA, standard.fun=NA ) for( i in 1:100 ) { t1 = profr( f(plot.grob,legend.object=legend.grob), interval=0.001 )[1,"time"] t2 = profr( plot(plot.grob), interval=0.001 )[1,"time"] times = rbind( times, c( t1, t2 ) ) } colMeans(na.omit(times)) modified.fun standard.fun 0.11435 0.19757 </code></pre> <p>My modified function takes about 40% less time, which makes sense since the <code>evaluate.legend</code> call takes about 40% of the run time in the <code>plot.trellis</code> example given in the question. There were also many other spots in the <code>plot.trellis</code> function where I could optimize speed. If one were to keep going, they could eventually get the function down to the bare bones so that the only the functions from the <code>grid</code> package are being called. This would essentially be rewriting an entirely new <code>plot.trellis</code> function with reduced flexibility but better speed. However, this is not what I wanted.</p> <p>On a side note, I noticed that the actual drawing of a plot on my screen takes longer than the run time reported by the profiling code. I used a stopwatch to time that it took a little less than 1 second longer for the plot to show up after the code was reported to be done running. I tried with other plotting packages that also rely on <code>grid</code> and found similar results. So, no matter how well a plotting function is optimized, I decided that either <code>grid</code>, base R, or my hardware eventually become the limiting factor when I get down to times of 1 second or less. There are solutions for this new problem but that's another topic...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload