Removing white space around R figures

February 21, 2013
By

(This article was first published on Hyndsight » R, and kindly contributed to R-bloggers)

When I want to insert figures generated in R into a LaTeX document, it looks better if I first remove the white space around the figure. Unfortunately, R does not make this easy as the graphs are generated to look good on a screen, not in a document.

There are two things that can be done to fix this problem.

First, you can reduce the white space generated by R. I use the following function when saving figures in R.

savepdf <- function(file, width=16, height=10)
{
  fname <- paste("figures/",file,".pdf",sep="")
  pdf(fname, width=width/2.54, height=height/2.54,
      pointsize=10)
  par(mgp=c(2.2,0.45,0), tcl=-0.4, mar=c(3.3,3.6,1.1,1.1))
}

The width and height are in centimetres. The ratio is about right for a beamer presentation, and also to fit two figures on an A4 page.

Then I use the commands

savepdf("filename")
# Plotting commands here
dev.off()

That will generate a pdf figure of about the right size and shape for a document, and with narrow margins of white space, and save it in my figures sub-directory.

The second trick is to trim the pdf files so there is no white space left. On a unix system, this is easily achieved as follows.

pdfcrop filename.pdf filename.pdf

There are probably windows and mac versions of the same, but I haven’t used them. Adobe Acrobat will also crop pdfs, but not from the command line as far as I know.

To apply pdfcrop to every file in a directory (using unix), save the following to a file called cropall.sh:

#!/bin/bash
 
for FILE in ./*.pdf; do
  pdfcrop "${FILE}" "${FILE}"
done

Make the file executable and run it.

In my post on Makefiles, I explain how to include pdfcrop within a Makefile.

If you just use pdfcrop without first reducing the white space in R, the proportions come out a little odd. So I tend to use both approaches together.

To leave a comment for the author, please follow the link and comment on his blog: Hyndsight » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.