Copy plot with grid.echo() from non-default device

2019-08-05 13:43发布

问题:

I want to copy a plot to a grid object from an svg device. grid.echo() docs says that it echoes the current device but drawn using grid graphics.

However, the following does not work:

svg()
plot(1:10)
print(dev.cur())
grid.echo()
a = grid.grab()
dev.off()
print(dev.cur())
grid.draw(a)

The printout from the above is:

svg 
  2 
null device 
          1 
Warning message:
In grid.echo.recordedplot(recordPlot(), newpage, prefix, device) :
  No graphics to replay

As you can see, the current device is the svg device right before grid.echo() is called, but still it finds nothing to echo. a returned by `grid.grab() is thus empty. I've tried the same thing with the pdf device with no success.

What am I doing wrong?

回答1:

Is this your expected behaviour?

library(gridGraphics)
svg()
plotfun <- function() plot(1:10)
print(dev.cur())
grid.echo(plotfun)
a = grid.grab()
dev.off()
print(dev.cur())
grid.draw(a)


标签: r graphics