Why are my graph files corrupted (jpeg, postscript, ...), or there is nothing in them (pictex)?

It seems like dev.off() makes all the difference. Turn off the device, and the info appears, perhaps from being released, beats the hell outta me.

How can I use R to output graphs for use in latex?

The most direct route is pictex(writefilename).

First, let's get some of the built-in data.

> data(presidents)

Then we set the output device, plot the data, and turn off the device.

> pictex("mytex.tex")
> plot(presidents)
> dev.off()

That file needs to be input into a properly formatted latex document. Make sure to include all of these packages:

\usepackage{graphicx}
\usepackage{pictex}
\usepackage{etex}

Put something like this in your document:

\begin{figure}[h!]
\center
\hspace{3ex}
\input{mytex.tex}
\end{figure}

Notice that \center does not work right for me here, so I'm forced to resort to some manual space manipulation. None of the various \begin{center} nor \centering seemed to help. Not yet sure why; notice below that \center works with \includegraphics{} and the .eps file; that might reflect on the different device outputs of R or it may just be the latex commands. (I would be exceedingly grateful to anyone who was willing to email me information concerning either the cause or the solution, potamite@usc.edu).

An eps can be output and imported as follows.

> postscript(file="myeps.eps", onefile=FALSE, horizontal=FALSE, width=4, height=4)
> plot(presidents)
> dev.off()

Now, you should be able to open the file independently (with mac's ps to pdf convertor), or you can include an eps in your latex like so:

\begin{figure}[h!]
\center
\includegraphics[width=.8\textwidth]{myeps.eps}
\end{figure}

Here, \center seems to work.

Be sure to include this package:

\usepackage{graphicx}

At first, I couldn't get jpgs to work, but try cutting and pasting (alt/opt-mouseclick) the following into your xterm window running R (notice that I finally left off the prompt symbol for more convenient cutting and pasting):

library(languageR)

dative.def=xtabs(~RealizationOfRecipient+DefinOfRec,data=dative)
dative.pron=xtabs(~RealizationOfRecipient+PronomOfRec,data=dative)

jpeg("datchi.jpg",width=400,height=420)
par(mfrow=c(1,2))
barplot(dative.def,beside=T,ylim=range(0,2500),legend=T)
barplot(dative.pron,beside=T,ylim=range(0,2500),legend=T)
dev.off()

Size specs seem important, also for including in latex, can use absolute size (or not, see above),
but definitely need .jpg (not .jpeg not .JPEG, ... ?!?)!

\begin{figure}[h!]
\center
\includegraphics[width=120mm]{datchi.jpg}
\end{figure}

How should I check out on-the-fly graphs?

I'm probably unaware of the lastest GUI advances for R, but X11 ain't so bad, and seems to be the default device anyways. Just make sure to open R from an xterm! (Or is there some way to appropriately configure my terminal? The earlier commands all worked just as well from a normal terminal window.)

>x11()
>plot(presidents)

No need to bother with .off() here either.

p.s. How do I cut and paste into an xterm window on mac os?

Use alt-mouseclick. From the xterm window, you can apple-c to grab and apple-v to paste in the native applications, but if you apple-c from the natives, you should use alt/option-mouseclick to paste into xterm.

Sometimes that's the simplest way to load a bunch of premade functions into R (as also recommended by Baayen).

BETTER YET: source('my-r-code')
(plus, everything seems to work fine through the terminal these days.)

The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees