在许多脚本中,我首先在屏幕上开发一个图形,然后需要将其保存为具有特定高度/宽度/分辨率的几种文件格式。用png(),pdf(),svg(),要打开一个设备,然后用dev.off()关闭它,我不得不将所有打开的设备调用放到我的脚本中,并将它们注释掉&每次只运行一个设备的代码。
我确实知道,对于ggplot图形,ggsave()使这更容易。对于基-R,和格子图形,我能做些什么来简化这个过程吗?
一个例子是:
png(filename="myplot.png", width=6, height=5, res=300, units="in")
# svg(filename="myplot.svg", width=6, height=5)
# pdf(filename="myplot.pdf", width=6, height=5)
op <- par() # set graphics parameters
plot() # do the plot
par(op)
dev.off() 发布于 2020-01-06 03:27:01
图形设备是grDevices包的一部分。使用多个开放设备的文档可能值得一读。据我所知,打开设备的循环数组被存储,但只有当前设备处于活动状态。因此,打开所有想要的设备,然后在它们上循环使用dev.list()可能是最好的选择。
# data for sample plot
x <- 1:5
y <- 5:1
# open devices
svg(filename="myplot.svg", width=6, height=5)
png(filename="myplot.png", width=6, height=5, res=300, units="in")
pdf()
# devices assigned an index that can be used to call them
dev.list()
svg png pdf
2 3 4
# loop through devices, not sure how to do this without calling plot() each time
# only dev.cur turned off and dev.next becomes dev.cur
for(d in dev.list()){plot(x,y); dev.off()}
# check that graphics device has returned to default null device
dev.cur()
null device
1
dev.list()
NULL
file.exists("myplot.svg")
[1] TRUE
file.exists("myplot.png")
[1] TRUE
file.exists("Rplots.pdf") # default name since none specified in creating pdf device
[1] TRUE您可以使用的文档中有很多。
https://stackoverflow.com/questions/59605378
复制相似问题