Re: [R] How to paste graph from R in Latex?

From: Jeremiah Rounds <>
Date: Sat, 17 May 2008 05:18:33 -0700

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

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





   \caption{Jittered pairs plot of severity predictors colored by red is severity 1.}


       \epsfig{file=bcpairs.png, bb= 0 0 800 800,width=5.25in, clip=}



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:


  \caption{Wine data pairs plots colored by cultivar.}




      \epsfig{file=ex2pairs.png, bb= 0 0 1200 1200,width=7in, clip=}



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:
> To:
> 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.
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.

Give to a good cause with every e-mail. Join the iím Initiative from Microsoft. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sat 17 May 2008 - 12:29:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 17 May 2008 - 13:30:38 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive