[R] How to paste graph from R in Latex?
Jeremiah Rounds
jeremiahrounds at hotmail.com
Sat May 17 14:18:33 CEST 2008
For school work I use png. Png files are more efficient size/quality wise than png, and also lend themselves to more generic application/viewing than ps.
In R this typically takes the form of:
setwd(...) #set working directory before starting any work typically at the top of scripts
... # stuff
png(filename,height=800, width=800)
#graphical commands
dev.off()
One of the great things about the png command is the size formatting. One great trick is to increase the size of the plotting area, plot, and then in latex shrink the graphic down. There is alot of graphics where this makes everything look better with very little work due to everything drawing at a finer resolution (in some lossy sense).
In your latex you will want to use package "epsfig" because under windows the png bounding box info isn't what default latex packages expect and epsfig can fix that easily.
Typically this has the form
\usepackage{epsfig}
\begin{document}
\begin{figure}[!htbp]
\center
\caption{Jittered pairs plot of severity predictors colored by red is severity 1.}
\label{bcpairs}
\epsfig{file=bcpairs.png, bb= 0 0 800 800,width=5.25in, clip=}
\end{figure}
\end{document}
The key line is \epsfig. bb = is the bounding box which corresponds to whatever you had in the png command in R. width is where you resize it. You supply the width and the package will 1 to 1 rescale it.
There are two tricks I picked up in my travels using this for homework. Well there are three, but I don't have example of the 3rd handy (side by side subfigures).
One is clipping a figure to get rid of a piece of it. That is a simple as changing the bb command to only bound the parts you want.
The other is shifting the graphic into the left margin a little bit. Handy for using the entire page on some graphics that just arnt easy to make any smaller.
That is done like so:
\begin{figure}[tbp]
\caption{Wine data pairs plots colored by cultivar.}
\label{winepairs}
\begin{minipage}{9in}
\hspace{-.75in}
\epsfig{file=ex2pairs.png, bb= 0 0 1200 1200,width=7in, clip=}
\end{minipage}
\end{figure}
The key there is you start a minipage and then shift it to the left. Note here the command in R was:
png("ex2pairs.png", height=1200, width=1200) for a large scatterplot.
A large scatterplot is an example of something that often looks better painted at a higher resolution, saved, and then shrunk down.
-------------------------------------
Someone mentioned Sweave. Sweaves value really depends on who you are and what your doing. Its work cycle is not appropriate for students or anyone that needs rapid cycle prototyping imo. Its great flaw is that it does not work well with "changing a little something--looking at the results in R" followed by "changing a little something in latex--looking at the results in dvi" repeated over and over and over again. The reason is it has to repeat far to much work in each cycle. Often times repeating long calculations.
This system you open a script in tinn-r. You run it. You have your texmaker open. You compilete your document. You dont like the graphic. You make your change to the plotting in your script. You highlight it and send it to r. You open it in a graphics viewer via double click or you simply compile your latex document again. Check it.
Sweave is not at all friendly to that "check your work as you go" mentality. It really needs a graphical interface that lets you indicate what not to redo, and just redo things incrementally.
> Date: Fri, 16 May 2008 18:24:00 -0700
> From: zroslina at yahoo.com
> To: R-help at r-project.org
> Subject: [R] How to paste graph from R in Latex?
>
> Dear R-expert,
> Is it possible to save graph from R into Latex document? I can see save as metafile , PNG, pdf etc, but I'm not sure which one to use.
> Thank you so much for your help.
>
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
_________________________________________________________________
Give to a good cause with every e-mail. Join the i’m Initiative from Microsoft.
More information about the R-help
mailing list