Jenn D.
Jenn D.

Reputation: 1055

Plotting to a file in R

I'm a complete newbie to R, and none of the introductions I've seen cover how to use R when all you've got is the command line and no windowing system. My data's on a server, and I'm working with it via ssh. In gnuplot, you can set your "display" to be a PNG file on disk. How do I plot something to a file on disk from R? R-2.9.1 on CentOS, if it matters. Thanks!

(Sorry if this is unusually basic, but I have the worst time Googling for quick answers with R. Cute name, impossible to search for.)

Upvotes: 7

Views: 14162

Answers (4)

amathew
amathew

Reputation: 366

Keep in mind that postscrpt(), pdf(), png(), and jpeg() have specific function parameters which can be used to customize the output.

For example:

postscript("filename.eps", horizontal=F, width=4, height=4, 
             paper="special", onefile=F)
plot(x)
dev.off()

check ?postscriptfor more information on the parameters that can be utilized.

Secondly, keep in mind that all commands that you want to be included in your saved plot should be executed prior to dev.off()

For example:

postscript("filename.eps", horizontal=F, width=4, height=4, 
             paper="special", onefile=F)
plot(x)    
text(5, 1, "This is a message for the aliens")
text(5, 0.5, "Pizza is tasty")
dev.off()

Another example:

regone <- glm(y ~ x1, data=mydata, family=...)
summary(regone)

postscript("filename.eps", horizontal=F, width=4, height=4, 
                 paper="special", onefile=F)
plot(x, y)
abline(regone)
dev.off()

Hope that helps.

Upvotes: 6

Aaron Statham
Aaron Statham

Reputation: 2068

If your connection to the server is fast enough, you could try X11 forwarding through your ssh connection - basically the server will plot to a window on your local screen, quite useful!

HOWTO no 1

HOWTO no 2

Upvotes: 2

nico
nico

Reputation: 51690

Just to expand on Gnoupi answer, you also need to close the connection to the device with dev.off if you want the plot to be written to file.

For instance

pdf("mygraph.pdf")
plot(x, y, "l")
dev.off()

Upvotes: 9

Gnoupi
Gnoupi

Reputation: 4754

From their documentation, it seems you have to use device drivers:

R can generate graphics (of varying levels of quality) on almost any type of display or printing device. Before this can begin, however, R needs to be informed what type of device it is dealing with. This is done by starting a device driver. The purpose of a device driver is to convert graphical instructions from R (“draw a line,” for example) into a form that the particular device can understand.

(...)

  • postscript() - For printing on PostScript printers, or creating PostScript graphics files.
  • pdf() - Produces a PDF file, which can also be included into PDF files.
  • png() - Produces a bitmap PNG file. (Not always available: see its help page.)
  • jpeg() - Produces a bitmap JPEG file, best used for image plots. (Not always available: see its help page.)

Upvotes: 3

Related Questions