Display name
Display name

Reputation: 4481

Multiple plots in a single row

From R for Data Science:

To put multiple plots in a single row I set the out.width to 50% for two plots, 33% for 3 plots, or 25% to 4 plots, and set fig.align = "default". Depending on what I’m trying to illustrate ( e.g. show data or show plot variations), I’ll also tweak fig.width, as discussed below.

How do I put multiple plots on a single row, using the method described above? I could use a package such as patchwork, but the purpose of this post is to understand what's being described above. The R Markdown below doesn't generate what I'd expect, two 'pressure' plots on the same row.

---
title: "Untitled"
author: "April 2018"
date: "4/11/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Text

```{r pressure1, echo=FALSE, fig.width=6, fig.asp=0.618, out.width="50%", fig.align="default"}
plot(pressure)
plot(pressure)
```

```{r pressure2, echo=FALSE, fig.width=6, fig.asp=0.618, out.width="50%", fig.align="default"}
plot(pressure)
```

R Markdown multiple plots single row

Upvotes: 2

Views: 3800

Answers (2)

Alejandro Navas
Alejandro Navas

Reputation: 13

You need to specify fig.show = "hold" along with out.width, as you can see here: https://bookdown.org/yihui/rmarkdown-cookbook/figures-side.html

Upvotes: 0

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

I’ve got no idea why your code (or rather, the code from R for Data Science) is not working. But plotting different data makes it work for me:

```{r pressure1, echo=FALSE, fig.width=6, fig.asp=0.618, out.width="50%", fig.align="default"}
plot(cars)
plot(pressure)
```

result screenshot

Alternatively, it seems to be enough to just specify different parameters, e.g.:

plot(pressure)
plot(pressure, main = '')

The fact that inconsequential changes fix the output indicates to me that this is a bug in RMarkdown.

That said, the easiest, most controlled way is to put par(mfrow = c(1, 2)) into the hunk directly before the plotting commands (or a ggplot2 solution such as faceting or {patchwork}).

Tweaking the alignment via the RMarkdown options can be tricky, due to spacing inserted by the conversion to HTML.

Upvotes: 1

Related Questions