Nested output devices in R?

Is it possible to build a graph on several output devices at once? I tried:

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

The plot of XY goes only to level3.png. (I have to go into all 3 files)

Oddly enough, the AB chart goes into level2.pdf, and I expected it to be written on level1.pdf, since level2.pdf should already be closed?

+5
source share
2 answers

, "" , . , , (/ ). , .

:

  • 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 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 
+4

ggplot2 print :

library(ggplot2)
p <- ggplot(data.frame(x=1:10, y=1:10), aes(x=x, y=y)) + geom_point()

pdf('a.pdf')
print(p)
dev.off()

png('b.png')
print(p)
dev.off()

, ggsave ( , , - Ubuntu):

ggsave('a.pdf', p)
ggsave('b.png', p)

"" , ggplot2 , .

+3

All Articles