How to Create Multicolumn Layouts in RMarkdown

Sample of Housing Report

In a lot of the consulting work that R for the Rest of Us does, we do complex layouts of the sort are typically done with page layout software like Adobe InDesign. For example, in the reports we did on demographic and housing data in Connecticut, the charts were laid out in a complex grid across multiple pages.

Sample of Housing Report

Or take a look at these reports, done in partnership with the Democracy Funders Collaborative’s Census Subgroup and ORS Impact, that provide an overview of efforts to promote the 2020 Census across the United States.

Sample page from reports on 202 Census outreach efforts

I’m often asked how we did these layouts. The truth is, it can be a bit complicated, and the answer varies depending on a number of factors. But, when an R in 3 Months participant asked this same question recently, I knew I had to come up with an answer to share.

Fortunately for me, I work with the very talented Charlie Hadley, who makes detailed videos explaining complex concepts to R learners. I asked Charlie to put together on some tips on the topic and she made some great videos showing how to make multicolumn layouts in RMarkdown. Here they are.

Option #1: Use patchwork or cowplot to combine multiple ggplot2 plots

There are two packages, patchwork and cowplot, that allow you to put multiple plots together. You can use this technique to make a multicolumn layout, as in this example:

Watch the video below as Charlie explains how this was made and follow her code below that.

Option #2: Use custom CSS in HTML files

If you’re knitting your RMarkdown documents to HTML reports, you’ll need to use custom CSS to add columns. You can do this using what’s known as flexbox or bootstrap.

The video below demonstrates the differences between these two approaches (code follows):

While HTML as an export format is common, many people work in organizations that want PDFs. Fortunately, the pagedown package (and Thomas Vroylandt and my pagedreport package) actually create HTML documents that they the convert to PDFs. As a result, you can use CSS to create multicolumn layouts in pagedown, as Charlie demonstrates:

Option #3: Use officedown and officer for multicolumn Word documents

If you need to export your RMarkdown document to Word, custom CSS won’t work. You can, of course, use option #1 to create multicolumn plots and then put those into Word. Another approach for Word documents is to use the officedown and officer packages. Together, these two packages allow you to build rich Word documents directly from RMarkdown. This video shows how to create two columns using output: officedown::rdocx_document.

Creating multicolumn layouts may seem like a small thing, but it can have a huge impact. Since the R for the Rest of Us team started using the techniques above, we’ve been able to create reports from start to finish in R. No longer do we need to bring in a graphic designer to do the final layout. It’s a huge timesaver and also makes it possible to do things like automatically make 170+ reports.

Have any questions? Put them below.