How to Size Plots in Beamer Slides Made Using R Markdown

Jesse John Feb 02, 2024
  1. Create Figures in Code Chunks in R
  2. Create a Beamer Presentation Using R Markdown
How to Size Plots in Beamer Slides Made Using R Markdown

In this article, we will see how to size the plots made by R Markdown in a Beamer presentation PDF file.

We will use R Studio to create the R Markdown file. The rmarkdown package is required to create the source file.

A LaTeX system is needed to generate PDF output from R Markdown. See Chapter 1 of R Markdown: The Definitive Guide for details.

We will use ggplot2 to generate the plots from code chunks in the same R Markdown file.

Plots created directly from code chunks in the R Markdown file are in vector graphics format in the rendered PDF file. Therefore, they are independent of resolution and scale well.

Create Figures in Code Chunks in R

  1. The figures are sized using the options fig.width and fig.height in the code chunk. The unit of measurement is inches.
  2. Use echo=FALSE to prevent the code chunk from printing.
  3. Use the fig.align option if required.

Load the required packages and data in a separate code chunk with the option include = FALSE. This will prevent R from printing messages from that code in the output.

Create a Beamer Presentation Using R Markdown

To create a new R Markdown file that will render as a Beamer presentation PDF file, choose File > New File > R Markdown....

Then choose Presentation and PDF (Beamer) in the pop-up window.

The following code chunk illustrates a complete .Rmd file, including the YAML header. However, the code has been indented to render correctly in the browser.

The plot background has been colored to show the full size of the plot.

Sample Code:

---
title: "Size Plots in Beamer Slides Made Using R Markdown"
author: "Author Name"
output: beamer_presentation
---

# Default

```{r include=FALSE}
# Load the Required Libraries
library(dplyr)
library(ggplot2)

# Create Some Data for the Plots
H = seq(from=0, to=1000, by=50)
set.seed(1001)
V = rnorm(21,10,3)
dfr = data.frame(H, V)
myplot = dfr %>% ggplot(aes(x=H, y=V)) + geom_line() + theme(plot.background = element_rect(fill="BurlyWood"))
```

```{r echo=FALSE}
myplot
```

# Square

```{r echo=FALSE, fig.width=2, fig.height=2}
myplot
```

# Rectangle

```{r echo=FALSE, fig.width=6, fig.height=4}
myplot
```

# Too Large: It Gets Cropped, It Is Not to Scale

```{r echo=FALSE, fig.width=20, fig.height=20}
myplot
```

# Center-Aligned Plot

We used `fig.align='center'` for this plot.

```{r echo=FALSE, fig.width=2, fig.height=1.5, fig.align='center'}
myplot
```

Alternatively, we can use `'left'` or `'right'`.
```

<!--adsense-->

Output:

A PDF file will be produced in the same folder as the source file. An image of the last slide is shown below.

![Image of the last slide](</img/R/LastSlide.webp>)

### a Troubleshooting Tip

In case the margins of the plots appear cropped, read about the `fig_crop` option in [R Markdown: The Definitive Guide](https://bookdown.org/yihui/rmarkdown/pdf-document.html).

Also, look at [issue #1365](https://github.com/yihui/knitr/issues/1365), referenced from the link above.
Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.