dev.hold, dev.flush and resizing windows

Solution 1:

As indicated by Dan Slone and gdkrmr viable option is to use intermediate raster file to plot complex graphics.

The flow is the follows:

  1. Save plot to png file.
  2. Plot the image into the screen device.

After this there will be no problems with refreshing and resizing.

Please see the code below:

# plotting through png
plot.png <- function(x, y) {
  require(png)
  tmp <- tempfile()
  png(tmp, width = 1920, height = 1080)
  plot(x, y, type = "l")
  dev.off()
  ima <- readPNG(tmp)
  op <- par(mar = rep(0, 4))
  plot(NULL, xlim = c(0, 100), ylim = c(0, 100), xaxs = "i", yaxs = "i")
  rasterImage(ima, 0, 0, 100, 100, interpolate = TRUE)
  par(op)
  unlink(tmp)
}

t <- 1:1e3
x <- t * sin(t)
y <- t * cos(t)


# without buffering
# plot(x, y, type = "l")

# with buffering in high-res PNG-file
plot.png(x, y)

Ouput: picture