Publication ready graphics in R
R is fantastic for making graphs and figures, but getting the output from the plotting window to a file that is acceptable for print publication can be challenging. Margins may need to be toyed with and the file itself has to be created at an acceptable resolution for printing. Most graphics intended for printing have a minimum 300 dots per inch (dpi), and this is often what the publisher will quote to the author. DPI refers to the printer's capability, while pixels per inch (ppi) refer to the software's capability. Obviously the author does not know the specific type of printer the publisher is using, thus we will provide a file specified in terms of ppi. The two terms "dpi" and "ppi" are used interchangeably but refer to different concepts. For practical use, this blog post uses the generic term resolution to avoid confusion between the two.
Assume we want a fixed 1200 resolution for our figures. This exceeds almost all publication requirements I have seen, and is therefore a conservative way to produce images that will be satisfactory for the publisher. In addition to the resolution requirements, publishers may also request figures in a specific file format, e.g., JPEG, PDF, TIF, PNG, etc. TIF files are fairly ubiquitous therefore this tutorial advocates for this format. Thus, the goal is to produce a 1200 resolute TIF image for publication. Note: there may be a more efficient way to do this within R, without the need for external software. If you have a better solution, please email me and I will update this post (and give you credit!). Nevertheless, this solution will work for the majority of researchers using R's built in graphic export and a third party imaging editing application. To start, format the figure in the R plotting window so it looks on screen how you would like it to look in print. After you have adjusted margins, labels, text size, etc., we need to encapsulate all of the plotting code with the following commands:
#output to tif for publication tiff("~/Figure1.tif",height=4,width=6,units='in',res=1200) [… all plotting commands go here …] #close file dev.off()
The first command, tiff(...), tells R to output the figure to a TIF file, 4x6 inches in size, at 1200 ppi resolution. You can easily change the directory and filename, but keep the aspect ratio (4x6) and the resolution (1200) as specified for landscape-oriented figures, or reversed as 6x4 for portrait-oriented figures. The second command, dev.off(), tells R you are done writing to the file. When writing to a file during plotting, the plot will not appear in the usual plotting window in R, instead it is written directly to the hard drive.
At this point a very large (hundreds of megabytes) file will be created. Now in the final step it needs to be "scaled" and compressed by graphics software. This could be done with Adobe Photoshop, the freely available GNU Image Manipulation Program (GIMP), or any equivalent software. Open the TIF image in the program of your choice, and select the menu option that corresponds to "Scale Image." When the dialog box opens, set both the X & Y resolution in pixels per inch (ppi) to 1200. Now the image will be scaled to print in 4x6 inch size at 1200 resolution (or 6x4 if your image is portrait orientation). Save the TIF image (choose LZW or JPG compression to shrink the file size if given the option), and forward the image file(s) to the publisher. If needed, they will further adjust the size depending on the desired print size for the actual article.