Open plots in a null device

為{幸葍}努か 提交于 2019-12-11 09:22:39

问题


I'm using the treemap package and I'm happy with how tmPlot arranges plot rectangles, I want to extract the grid for my own different plots. An example is as follows:

library(treemap)
data(GNI2010)
dat <- tmPlot(GNI2010,
          index=c("continent", "iso3"),
          vSize="population",
          vColor="GNI",
          type="value")[[1]][[1]]

This way I can store the coordinates of the rectangles I want. The catch is that it produces a plot as well. I can see a couple of ways to prevent the plot from being produced:

.Call("R_GD_nullDevice", PACKAGE = "grDevices")
#tmPlot here
dev.off()

This would essentially send the plot to a NULL device, but it gives a warning:

R_GD_nullDevice is deprecated and will be removed shortly 

I'd rather my code didn't break this way. I could also strip out the relevant parts of tmPlot so that only the parts I wanted. This is possible, but would be a bit of a nuisance. I intend that a function containing this goes inside a package.

In short, is it possible to suppress graphics?


回答1:


One way that seems to work is to open a NULL pdf device. I originally tried this with the png device, which doesn't work.

pdf(NULL)
dat <- tmPlot(GNI2010,
          index=c("continent", "iso3"),
          vSize="population",
          vColor="GNI",
          type="value")[[1]][[1]]
dev.off()


来源:https://stackoverflow.com/questions/14725620/open-plots-in-a-null-device

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