问题
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