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

最后都变了- 提交于 2019-12-11 09:03:17

问题


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)


来源:https://stackoverflow.com/questions/55280746/copy-plot-with-grid-echo-from-non-default-device

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!