Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

r - Saving plot as pdf and simultaneously display it in the window (x11)

I have written a function that creates a barplot. I would like to save this plot as a pdf as well as display it on my screen (x11) when applying this function. The code looks like this.

create.barplots <- function(vec)
 {
   x11()                                  # opens the window
   ### Here is a code that creates a barplot and works perfectly
   ### but irrelevant for my question
   dev.copy(pdf("barplots.table.2.pdf")) # is supposed to copy the plot in pdf
                                         # under the name "barplots.table.2.pdf"
   dev.off()                             # is supposed to close the pdf device
 }

This creates the following error: 'device' should be a function

When I modify the code to:

create.barplots <- function(vec)
 {
   x11()
   ### Here is a code that creates a barplot and works perfectly
   ### but irrelevant for my question
   dev.copy(pdf) # This is the only difference to the code above
   dev.off()
 }

R displays the plot and creates a file called Rplots.pdf. This is a problem because of several reasons.

I also tried to open the devices the other way around. First open the pdf device, than copy the content of the pdf device into the x11 device, than set the pdf device as active and than close the pdf device. The code here looks like this:

create.barplots <- function(vec)
 {
   pdf("barplots.table.2.pdf") # open the pdf device
   ### Here is a code that creates a barplot and works perfectly
   ### but irrelevant for my question
   dev.copy(x11)              # copy the content of the pdf device into the x11 device
   dev.set(which = 2)         # set the pdf device as actice
   dev.off()                  # close the pdf device
 }

The problem here is that the wondow that is supposed to display the plot is empty!

To sum up, I have two questions: 1) How to save a plot as pdf and display it in x11 simultaneously? And 2) How to save the plot not in the working directory somewhere else?

EDIT

The solutions above work great. But I still do not understand why

pdf("barplots.table.2")
barplot(something)
dev.copy(x11)

displays an empty grey window instead of copying the content of the pdf device in the window device! I also tried

pdf("barplots.table.2")
barplot(something)
dev.copy(window)

In which I failed as well...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about:

create.barplots <- function(...) {
  x11()
  plot.barplots(...) # create the barplot
  dev.copy2pdf(file = "path/to/barplots.table.2.pdf")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...