22

I need to save the plot as .png and show the plot at the same time without duplicating the code. Is there an elegant way to do it? Working on RStudio for MAC.

I can get it to work as bellow but I don't like it.

#Step1: save the plot
png("myplot.png")
#plot code
dev.off()

#Step2: to display the plot
#plot code (again!) to display it in RStudio

Cheers, I.M.

1

4 Answers 4

23

I found the previous answers as being incomplete. Drilled down on how to display the plot in the default RStudio "Plots" window and save the plot in "png" at the same time.

In case that someday, someone might come across the same question, here is how is being done, line by line:

R:> 
R:> dev.list()                  # fresh start. no graphical devices available at this point
NULL
R:> dev.cur()                   # no current device at this point
null device 
      1 
R:> dev.new()                   # open graphical devices
NULL
R:> dev.list()                  # list them
     RStudioGD quartz_off_screen 
        2                 3 
R:> png("plot50.png")           # open an offscreen device as png. New device should be number 4
R:> dev.list()                  # the list of all graphical devices includes the newly created device, number 4
    RStudioGD quartz_off_screen quartz_off_screen 
          2          3               4 
R:> dev.cur()                   # NOTE: the new created device(number 4) becomes "current" automatically,
quartz_off_screen               # as soon as it has been created
            4 
R:> dev.set(which = 2)          # switch back to device 2 used to display the plot in the default RStudio 
                                # "Plots" window
RStudioGD 
    2 
R:> dev.cur()                   # indeed, RstudioGD becomes the current device after the switch step from above
RStudioGD 
    2 
R:> dev.list()                  # just a check on all available devices. device 4 still in the list after 
                                # the switch
    RStudioGD quartz_off_screen quartz_off_screen 
            2           3                 4 
R:> plot(c(1:100))              # plot an example. It will be displayed in "Plots" window of RStudio
R:> dev.list()                  # just a check on all the available devices
   RStudioGD quartz_off_screen quartz_off_screen 
      2                 3                 4 
R:> dev.copy(which = 4)         # copies from current device(RStudioGD) to device 4. It automatically sets
   quartz_off_screen            # device 4 as current
         4 
R:> dev.cur()                   # indeed , device 4 is the current device
  quartz_off_screen 
         4 
R:> dev.off()                   # close device 4. IMPORTANT: AT THIS POINT the plot is saved as
    RStudioGD                   # png("plot50.png") in the current working directory.
                                # Three actions takes place at this point, all at once:
                                # 1. closes device 4
                                # 2. save the plot as "plot50.png"
                                # 3. sets the dev.next() (which is RStudioGD) as the current device
       2 
R:> dev.cur()                   # RStudioGD becomes current as soon as device 4 has been closed down.
   RStudioGD 
       2 
R:> 
Sign up to request clarification or add additional context in comments.

3 Comments

what happened to device 3? I'm trying to create a postscript and R is opening a png devic, but not closing it. it seems to have written properly to the specified ps file, but I don't like that this png device is floating around.
Not sure that I understood your question. You want device 3 to be closed as soon as you open a new device? If that's the case, the answer is that you have to manually close the devices opened previously.
The problem is basically that referring to devices by number can lead to issue--it's not always easy to keep track of which device is at which number. I ultimately got around this by using which=dev.list()["png"] and its analogues. This may need some further tweaking for those working with multiple png devices, but it's a start.
4

To add to the great answer by flamenco, here is hence a simple version which seems to work out of RStudio. Assume you've plotted something inside RStudio, and then want to save that same thing.

pdf(file = "xyz.pdf")
dev.set(which = 2)
dev.copy(which = 4)
dev.off()

I've tried using dev.off() repeated, this workflow seems to be quite stable.

Comments

2

I found an answer here:

If you follow the process in the previous section, you'll first have to make a plot to the screen, then re-enter the commands to save your plot to a file. R also provides the dev.copy command, to copy the contents of the graph window to a file without having to re-enter the commands. For most plots, things will be fine, but sometimes translating what was on the screen into a different format doesn't look as nice as it should.

To use this approach, first produce your graph in the usual way. When you're happy with the way it looks, call dev.copy, passing it the driver you want to use, the file name to store it in, and any other arguments appropriate to the driver.

The example they give using dev.copy is the following:

dev.copy(png,'myplot.png')
dev.off()

Comments

0

I stumbled upon this post when looking researching a similar problem. I was having trouble saving a plot in R running in a Jupyter notebook. I am posting my solution for completeness. You use the sequence of pdf("myplot.pdf") (or png or jpeg, ...), plot code, and dev.off(). You must do all of this in the same cell of your Jupyter notebook. Here is what worked for me (all in the same cell).

pdf("myplot.pdf")
# Plot commands here 
dev.off()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.