tags:

views:

97

answers:

4

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

+3  A: 

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.)
Gnoupi
Remember to close the file with dev.off()! If you don't use dev.off(), the file will be created but never written to, and you'll get an error.
thebackhand
+6  A: 

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()
nico
Ah. An actual example of device usage was just what I was looking for. Thanks!
Jenn D.
Thank you for giving an example, I only quoted the manual, didn't know how to use it.
Gnoupi
+2  A: 

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

Aaron Statham
Yeah, not what I need; but handy links for other situations. Thanks!
Jenn D.
+1  A: 

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.

Ah; even better examples, with some handy tips. Thanks a bunch! (Now of course I need to get some friendly admin to actually put the device drivers in place on our server.)
Jenn D.