Skip to content

Publication quality graphics in R

6 messages · Mark Albins, David Hewitt, Glen A Sargeant +3 more

#
R-sig-eco list,

This is a bit of a tangent from the current conversation, but can  
someone elaborate on
this quote from the following message,

"Plots in R come out so nicely, publication quality if you specify  
them correctly."

In particular, I'd like to hear from the list, how folks specify and  
export presentation
quality and publication quality graphics with R.  I've had problems  
when exporting
graphics using the copy-to-clipboard option (both bitmap and metafile)  
and also when
saving them as jpgs.  They almost always seem to look a little funny  
(e.g. pixelation,
symbols coming out distorted etc.).  The only option that I've had  
much success with is
saving them as pdf's, but that format is less than ideal when trying  
to incorporate a
graphic into another document (e.g. Word or Powerpoint), and is often  
not the format
requested by journals.

Any advice would be appreciated.

Thanks,

Mark

__________________________________________________

Message: 1
Date: Thu, 29 May 2008 20:21:54 -0400
From: Jessi Brown <jlbrown at unr.edu>
Subject: [R-sig-eco] AIC, R-Mark, and nest survival
To: r-sig-ecology at r-project.org
Message-ID: <483F48A2.9040906 at unr.edu>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi, Dave. Thanks for pointing out the merits of R-Mark as far as
generating AIC tables reflecting the results of nest survival and other
data model types.

I do indeed use R-Mark for CJS and multistate population modeling, but I
prefer the logistic exposure/"Shaffer" nest modeling paradigm for a
number of reasons. When you have something of a background in linear
models, the GLM approach is perhaps a little more intuitive than Program
MARK (but R-Mark circumvents some of that), and data preparation and
covariate handling seems to go more quickly and easily. Plots in R come
out so nicely, publication quality if you specify them correctly. Also,
there's capacity for extending the logistic-exposure models to mixed
models (which might not be a wise decision, based on violation of the
assumption that the mean of the error distribution is equal to zero, but
I digress).

I've done nest survival with both Program MARK (not R-Mark) and GLMs in
R, and it seems to me (not a biostatistician, but an ecologist who
dabbles with statistical tools), that it's ok to just go with whatever
suits your particular style. In my case, since I tend to start with (and
retain) fairly focused, restricted model suites, it doesn't bother me
much to hand construct AIC tables with the "n-effective" calculated AIC
values after having run the GLMs.

BTW, if anyone needs a script of how to set up the logistic-exposure
link function, it's among the examples in help(family).

cheers, Jessi Brown
#
I'd strongly recommend making the figures by writing them to files rather
than copying and pasting from the graphics device window. So, for example,
when you want a figure as a pdf you'd use "pdf(filename, options)" to open
the device with the output settings you desire, then do all your plotting
commands, then close the device with dev.off().

type ?pdf at the prompt; other devices are described in ?png

You can run the plots in the device window and then save as..., but you'll
get subtle (and sometimes not-so-subtle) differences between the window
display and the output through a device command. Once you get what you like,
then fine-tune the device options to produce the final file to be imported
to your text.

The format you use will vary depending on what you're putting it into. I've
had the most luck with PDF images rolled into LaTeX PDFs (via LyX --
www.lyx.org), although I've heard that EPS is even better for some things.
JPGs never worked well for me in anything. I gave up on M$ Office a few
years ago. Not much looks good in there.


-----
David Hewitt
Research Fishery Biologist
USGS Klamath Falls Field Station (USA)
#
You can exert quite a lot of control over the appearance of JPG output. 
Try inserting "lowres.jpg" and "hires.jpg" (generated by the code below) 
into a Word document and comparing the results.

foo <- function(){
plot.new()
plot.window(c(0,1),c(0,1))
plot(sin(seq(0,2*pi,0.01)),type="l",col="blue",lwd=2)
title("Example")}

jpeg("lowres.jpg",width=480,height=480)
foo()
dev.off()

jpeg("hires.jpg",width=4*480,height=4*480,pointsize=48)
foo()
dev.off()

*************************************************
Glen A. Sargeant, Ph.D.
Research Wildlife Biologist/Statistician
Northern Prairie Wildlife Research Center
8711 37th Street SE
Jamestown, ND  58401

Phone: (701) 253-5528
E-mail:  glen_sargeant at usgs.gov
FAX:     (701) 253-5553
*************************************************



David Hewitt <dhewitt37 at gmail.com> 
Sent by: r-sig-ecology-bounces at r-project.org
05/30/2008 04:06 PM

To
r-sig-ecology at r-project.org
cc

Subject
Re: [R-sig-eco] Publication quality graphics in R
presentation
Powerpoint),
I'd strongly recommend making the figures by writing them to files rather
than copying and pasting from the graphics device window. So, for example,
when you want a figure as a pdf you'd use "pdf(filename, options)" to open
the device with the output settings you desire, then do all your plotting
commands, then close the device with dev.off().

type ?pdf at the prompt; other devices are described in ?png

You can run the plots in the device window and then save as..., but you'll
get subtle (and sometimes not-so-subtle) differences between the window
display and the output through a device command. Once you get what you 
like,
then fine-tune the device options to produce the final file to be imported
to your text.

The format you use will vary depending on what you're putting it into. 
I've
had the most luck with PDF images rolled into LaTeX PDFs (via LyX --
www.lyx.org), although I've heard that EPS is even better for some things.
JPGs never worked well for me in anything. I gave up on M$ Office a few
years ago. Not much looks good in there.


-----
David Hewitt
Research Fishery Biologist
USGS Klamath Falls Field Station (USA)
#
Hi Mark,

I second writing to files as David says, but I have a few things to add:

- jpg is meant to encode photos, and because of the compression it uses 
it will butcher complicated text, especially if you have to re-save 
multiple times, resize the images, etc...  PNG avoids this problem but 
still compresses photo-like images nicely (say colored 3-D plots).  MS 
Office will definitely accept PNG.

- postscript is probably best for simpler figures (I think MS Office 
accepts it happily) and journals should be happy to take it (?)

- when you open the PNG/pdf/postscript device for writing an image, it 
helps to already know what size/resolution the image needs to be because 
resizing will almost certainly alter the look of text. I tend to save 
data frames for making particular figures instead of saving images for 
this reason.

- I haven't had trouble with this, but the ?postscript help page 
mentions that if you use complex symbols, you need to make sure you have 
good fonts/encodings set for them--see the encodings section on 
?postscript...

- The R wiki has some good information on preparing images.

Hope that helps,

Krzysztof
Mark A. Albins wrote:
#
Hi Mark,

as previous people have stated ps and png are good options.  Another  
option is the tiff format.  In the past you had to install the Cairo  
package and use function Cairo().  As of R 2.7.0 there is now the  
function tiff() to create tiffs, it looks like the Cairo functions  
have become part of R.  Either way you need to install Cairo  
separately outside of R.  Also expect to get some interesting  
formatting issues when you create high res (300 dpi) graphics (e.g.,  
you need to scale text including axes appropriately, see: ?par).  A  
nice feature is that you can specify the graphic size in inches so use  
~7 in wide for full page or ~3.5 in wide for single column (check your  
journal's style guide).  Note that there will be some interesting  
artifacts due to your screen probably being 72 dpi, print out a copy  
before you submit it to make sure the print version looks like what  
you want (a 300 dpi image looks huge on my monitor).

I've been putting R graphics on my website to keep my committee  
informed.  If you'd like to see some examples see (everything except  
the map on the webpage is R output):

http://oregonstate.edu/~knausb/pubs/asle_aflp/asle_aflp.html

The downside of tiff is that they're big, the good side is that you  
can edit the tiff in your favorite graphics package (e.g., the GIMP).

Enjoy!
#
"Mark A. Albins"
<albinsm at science.oregonstate.edu> writes:
A simple way to get consistent results is to open the graphics window
with explicit dimensions, create your figure, and then copy the window
to eps:

X11(width = 2, height = 3) ## final print size will be 2" by 3"
plot(Y ~ X)
points ...

dev.copy2eps(file="...")

This way you see everything you've done, and you save copy to file in a
format that preserves all the detail. Never resize the figure by
dragging the corners at any point in processing, or you'll distort all
the proportions. This is really important if you're working with
ordinations of any kind, where the distances between points is key.

It's important to understand the differences between the different
formats. eps, ps, pdf, and wmf are all vector formats. That means they
can be reproduced with arbitrarily high or low resolution, so long as
the printer can read them properly, while remaining relatively small
files. jpg, png, tiff, bmp are all raster formats. That means they are
limited to the resolution they are created at, and if they were created
at high resolution they will be very large files.

If the printer can't deal with your eps, which is a problem I ran into,
you can convert it into a suitably high-res tiff with image editing
software like the GIMP. If you have a low-res jpg (which will look
pixelated), you can't fix it, you have to re-make it at higher res, or
in another format. Of course, you can generate high-res tiff or jpg
files to begin with, but they can be cumbersomely large to email around.

my 2 cents!

Tyler