The four steps to publication-grade graphics in R

Reading time ~2 minutes

For many, the main reason to use R is to generate really good-looking or at least informative graphics. However, while it is easy to find information on how to make an individual plot, it can take some time to find out how to get them out into the world. Here is my four-step program to turning your plot into a graphic-file.

In the following I will use my present favorite plot from here as an example.

1. Set your options

R allows you to set many general options for your plots, e.g. the margins and whether or not a box should be drawn around most of which are the documentation here.

My favorites are:

  • mfrow: To combine several plots into one (not necessary for the exaple).
  • mar: To control the margins of the plot (not necessary for the exaple).
  • las: To rotate the axis-labels (not necessary for the example)
> > > > par(mar=c(2,0,2,2)) > > > >

2. Make your plot

Well this part is the most heterogeneous, just take a peek at the gallery to get some inspiration, or dive into ggplot2 for a very comprehensive graphic-framework that also helps you to add legends.

> > pie(c(1,1), labels="", col=c("black", "white"), main="Your options according to Yoda", init.angle=90) > >

3. Add a legend

R has a built-in function to add legends. The full documentation can be found here,  The options I use almost every time are:

  • x,y: To tell R where to put the legend. Usually I use the  name for the location (e.g. “top left”), instead of x and y-coordinates.

  • legend: To add some descriptions for the colors/line-types/shadings.

  • fill: To select the colors or alternatively “lty” for the line-type

  • bty: To get rid of the box around the legend

> > legend("right", c("do", "do not", "try"), fill=c("black", "white", "gold"), bty="n", cex=1.4) > >

4. Save it to a file

R has various options to save files, as documented here. I most often save them as png, as the file-size for tiffs is extremely large at the same quality. This allows you to set the options

  • filename: well something with a  ”.png” at the end

  • width and height: To control the scale and of the image.

  • units: To

  • resolution: To Journals love images with at least 300 dpi.

  • bg: To have non-transparent background simply use “white”.

> > dev.print(png, "yoda.png", width=8, height=6, units="in", res=300, bg="white") > >

Enjoy the complete script

par(mar=c(2,0,2,2))
pie(c(1,1), labels="", col=c("black", "white"), main="Your options according to Yoda", init.angle=90)
legend("right", c("do", "do not", "try"), fill=c("black", "white", "gold"), bty="n", cex=1.4)
dev.print(png, "yoda.png", width=8, height=6, units="in", res=300, bg="white")

The world is flat F(1,18) = 39.200; p = .335 - or p < .01 or p <.001? - Check your stats!

A reviewers dream has come true. The new __statcheck__-package for [R](r-project.org) automagically checks the accurate __reporting__ of ...… Continue reading

Publication-lists-4-Your-Website

Published on June 19, 2015

Relaunch on Jekyll

Published on June 04, 2015