Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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...

like image 851
Hagen Brenner Avatar asked Nov 08 '11 23:11

Hagen Brenner


People also ask

How do I save a plot as a PDF?

Plot the data frame with 'o' and 'rx' style. To save the file in PDF format, use savefig() method where the image name is myImagePDF. pdf, format = ”pdf”. To show the image, use the plt.

Which R function will save your plot as a PDF file?

Once you've created a plot in R, you may wish to save it to a file so you can use it in another document. To do this, you'll use either the pdf() , png() or jpeg() functions. These functions will save your plot to either a . pdf, .

How do I save multiple plots as PDF in R?

To save multiple plots to the same page in the PDF file, we use the par() function to create a grid and then add plots to the grid. In this way, all the plots are saved on the same page of the pdf file. We use the mfrow argument to the par() function to create the desired grid.

How do you save a graph to file displays and close graphs in R?

Plots panel –> Export –> Save as Image or Save as PDF It's also possible to save the graph using R codes as follow: Specify files to save your image using a function such as jpeg(), png(), svg() or pdf(). Additional argument indicating the width and the height of the image can be also used. Create the plot.


2 Answers

How about:

create.barplots <- function(...) {
  x11()
  plot.barplots(...) # create the barplot
  dev.copy2pdf(file = "path/to/barplots.table.2.pdf")
}
like image 114
Max Gasner Avatar answered Nov 11 '22 17:11

Max Gasner


You can easily add arguments for pdf in the dev.copy call, like this:

create.barplots <- function(vec,dir,file)
 {
   windows()
   plot(vec)
   dev.copy(pdf,file=paste(dir,file,sep="/") 
   dev.off()
 }

dev.copy() has a ... argument to pass arguments to the pdf function, see also ?dev.copy. Alternatively you can use dev.copy2pdf , as Max told you. I'd also advise you to use windows() instead of x11(), otherwise you might have trouble with the font families. The defaults for x11 and pdf don't always match.

To save a file in another directory, just add the full directory (eg with paste, like in the function above)

like image 23
Joris Meys Avatar answered Nov 11 '22 15:11

Joris Meys