How I produce ‘publication ready’ tables and figures using R

R

18 Sep 2022

Modified:

9 Sep 2023

I often need to produce tables and figures suitable for submission to journals. This means:

  1. tables in Word document(s)
  2. figures in an editable vector-based format (e.g. PDF)
  3. a mixture of tables, figures and text in Word document(s) (e.g. for a supplementary file).

To automate this process (and make it reproducible!) within R I use the following packages:

In this post I give a brief overview of how I use these package in R to produce tables and figures.

Current methods

Creating figures

To create figures (i.e. plots) I pretty much always use ggplot2. I have also developed a package, ckbplotr, which helps to create plots (in particular, forest plots) in the required style.

Laying out figure panels

There are a few packages available for combining separate plots into one figure, such as gridExtra, patchwork, egg, and cowplot. My requirements are usually modest, so I find using arrangeGrob() from gridExtra sufficient.

Adding titles and footers and saving as PDF

I also use arrangeGrob() to add margins to plots, then textbox_grob() from gridtext to add titles and footers. The final figure can then be saved to a PDF file using ggsave(). This approach makes it easy to ensure consistent formatting and positioning of titles and footers across multiple figures. ckbplotr has a function save_figure() to automate these steps.

Saving figures as images

If I’m going to use a figure in a Word document (via R markdown, see below) then I will either use saveRDS() to save the plot object to a .rds file, or use ggsave() to save it as an image (which could also be used for posters, presentations etc.).

Creating tables

To create tables, I usually use the necessary tidyverse and base R functions to create a data frame/tibble with the correct content. (I avoid using packages which combine formatting of cell content (e.g. rounding and display of numbers) with creating the actual table.) Then I use flextable to format the table appropriately. I’ve found flextable to be the only package which allows me suitable control of table formatting for output in a Word document. I use custom functions to consistently and conveniently apply some of the steps.

I use saveRDS() to save the flextable object to a .rds file, which can then be included in an R markdown document using readRDS().

Word documents

R markdown documents are the primary method for creating reproducible documents using R. My R markdown documents contain text (titles, footers, paragraphs), tables produced with flextable, and plots.

For better control of layout, text styles, etc. I use the officedown and officer packages. These allow me to use sections and styles in Word, so that I can have pages with different orientations and margins in the same document, as well as text formatted exactly as needed. I’ve found using a suitable template is crucial to getting the formatting I need. (For PDF or HTML output, other methods such as directly using LaTeX and HTML code would be a suitable method.)

The future

My methods for automating the production of tables and figures using R, and the packages I use, are continually evolving. As new functions and packages become available, or I find better ways of doing things, I change and adapt my approach. Each project or paper also often has some tweak or customisation needed.