Saving Plot as PDF and Simultaneously Display It in the Window (X11)

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

How about:

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

Display and save the plot simultaneously in R, RStudio

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

Show graph on display and save it to file simultaneously in gnuplot

If you want to send a plot both to a file and to an interactive terminal like x11 or wxt you have the replot after you changed the terminal

set terminal png
set output 'file.png'

plot sin(x)

set terminal x11
set output
replot

If you don't want to set the x11 terminal explicitely, but rather use the default terminal, whatever it is, you can use the special terminals push and pop so save and restore a terminal:

set terminal push
set terminal pngcairo
set output 'file.png'
plot sin(x)
set terminal pop
set output
replot

To make this more transparent and save any image after you plotted it to an interactive terminal you could define a gnuplot script export.gp which you can then call and give the output file name as parameter.

The export.gp script is

set terminal push
set terminal pngcairo
set output '$0'

replot
set output
set terminal pop

which you can then use as

plot sin(x)
call 'export.gp' 'test.png'

Note, however, that the exported file and the plot shown in the interactive window will be different, but if you use wxt as interactive and pngcairo or pdfcairo as output terminals, the chances are quite high, that displayed and exported images are very similar.

With gnuplot 5.0 the qt and wxt terminals offer an "Export" button to save exactly the image shown in the window as svg, pdf or png files. Unfortunately, this functionality cannot yet be invoked from a script, i.e. there is no export command.

Plot and Error opening the file. This file is already open or in use by another application.

You can't overwrite files like myImage.pdf while they are opened by another application, like your pdf viewer. I suggest creating temporary files, which goes easy:

pdf(file = tf <- tempfile(fileext = ".pdf"))
plot(g)
dev.off()
shell.exec(tf)

R dev.copy() with non-GUI devices for batch graphic scripts

You need to call dev.control('enable'). From ?dev.copy:

dev.copy copies the graphics contents of the current device to the
device specified...(If recording is off on the current device, there
are no contents to copy: this will result in no plot or an empty
plot.) ... The displaylist can be turned on and off using dev.control.
Initially recording is on for screen devices, and off for print
devices.

Printing to a plot device created by a function

Your code works as it's supposed to, except for the default values.

From ?pdf:

width, height: the width and height of the graphics region in inches. The default values are 7.

You're creating a document of size 40 by 40 feet.

Fail to display any image via X11

Having a blank window is the correct behaviour for calling X11(). Usually, you won't need to call that function, but it means that you can specify how tall/wide the plot window is before you create a plot.

If you still have a blank window after you try and plot something, then you are probably writing to a different device.

Have you opened another device (maybe with png, etc.) and forgotten to close it?

What does dev.cur() return?

A reproducible example of this:

png("foo.png")
x11()
dev.set(dev.list()[names(dev.list()) == "png:foo.png"])
plot(1:10)

#Make sure you call this afterwards
graphics.off()


Related Topics



Leave a reply



Submit