简体   繁体   中英

How do I create an “interactive” graphics device based on off-screen (bitmap) graphics in R?

Plotting large plots in R can be painfully slow. I'm trying some workarounds but even with the maximally-buffered "dbcairo" X11 device, plotting seems to take much longer than it could. I noticed that the "png" device is faster for both small plots and large plots.

Small plots, png() beats X11() by about 3x:

> system.time({X11(type="dbcairo"); plot(1:1e3); dev.off()})
   user  system elapsed 
  0.234   0.029   0.373 
> system.time({png("file.png",type="cairo"); plot(1:1e3); dev.off()})
   user  system elapsed 
  0.114   0.000   0.113 
> system.time({png("file.png",type="Xlib"); plot(1:1e3); dev.off()})
   user  system elapsed 
  0.056   0.000   0.107 

Large plots, png() beats X11() by 2.3x to 46x:

> system.time({X11(type="dbcairo"); plot(1:1e5); dev.off()})
   user  system elapsed 
 14.420   0.157  15.491 
> system.time({png("file.png",type="cairo"); plot(1:1e5); dev.off()})
   user  system elapsed 
  6.790   0.001   6.826 
> system.time({png("file.png",type="Xlib"); plot(1:1e5); dev.off()})
   user  system elapsed 
  0.144   0.010   0.340 

The results were pretty much the same with other X11() device types (being similar to "dbcairo") and other bitmap image types (being similar to "png"). I'm guessing png() type "cairo" takes longer than "Xlib" because it produces antialiased output.

With a medium-sized ggplot2 plot, I found that png() is 1.7x to 2.6x faster than X11() (that's using png(..); plot(g); dev.off() , not ggsave() )

Since I can load and reload a PNG file almost instantaneously using a minimalist image-viewer like "feh", I'm wondering why I don't use png() as my primary plotting device with R.

The problem of course is that the PNG file isn't written to disk until I call dev.off(). This crimps the style of the standard plotting interface, where I set the device once and then bring up various plots, sometimes adding lines or text, while viewing each change immediately in the plot window.

Would it be difficult to create a new R graphics device which writes image files to PNG (or some other image format), but uses a simple image viewer like "feh" to display them after each plotting command? In other words, I'm looking for an "interactive" plotting device like X11, but which uses the bitmap off-screen rendering facilities of existing devices such as png(), jpeg(), or tiff() to draw the image. Or maybe the png() device can be modified to have an option which gives it this behavior. (Or maybe I should be using knitR for everything ... but I am more familiar with the command line ...)

If X11 drives you up the wall and you don't want to use the RStudio plot viewer (or can't because you're on a remote server where you can't install RStudio Server), one interesting alternative is to use the rmote package. You can install it on either your own local R installation or, if you're SSHing into a remote machine and are willing to forward the port, on a remote R instance. Then any plots and help files you print will be redirected to a local web server, where you can access them from your browser.

It also comes with some nice extras, like:

  • A plot history,
  • The ability to right-click and save plots, as you would any other image in a browser, and
  • The ability to choose which kinds of output are sent to the server.

I've found this a nice alternative to X11 forwarding with plots on a remote server, and it works well locally too 🙂

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM