是否可以一次将图形绘制到多个输出设备中?我试过:
getwd()
pdf("level1.pdf")
pdf("level2.pdf")
png("level3.png")
x=1:10
y=1:10
plot(x, y)
dev.off() # close level3
dev.off() # close level2
a=10:20
b=-10:0
plot(a,b)
dev.off() # close level1
XY 图只进入
level3.png
. (我应该进入所有 3 个文件)奇怪的是,AB 情节进入 level2.pdf,而我希望它被写入 level1.pdf,因为 level2.pdf 应该已经关闭了?
最佳答案
可以同时打开多个设备,*但仅限
一台设备当前 “活跃” 并将所有图形输出发送到该设备。所以不,您不能一次将图形绘制到多个输出设备中(我的意思是并行/同时)。在这里,我详细介绍了您可以使用的设备类的一些方便的功能。
您可以使用以下功能:
dev.List()
: t 获取打开设备列表 dev.cur()
获取当前事件设备 dev.set()
更改事件设备 dev.next()
和 dev.prev()
: 在设备列表中创建下一个/上一个设备 例如:
pdf("level1.pdf")
pdf("level2.pdf")
png("level3.png")
## list the devices
dev.list()
pdf pdf png:level3.png
2 3 4
## current device
dev.cur()
png:level3.png ## that's why The XY plot goes only into this device
4
### this will go in the current device
x=1:10
y=1:10
plot(x, y)
## change the active device
dev.set(dev.next())
pdf
2
### close all devices
graphics.off()
## list the devices
dev.list()
NULL
因此,将其应用于您的示例:
pdf("level1.pdf")
pdf("level2.pdf")
png("level3.png")
dev.off() # close level3
dev.off() # close level2
dev.cur()
pdf ## plot A,B goes on this device
3
关于r - 在 R 中嵌套输出设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15084513/