Search code examples
r-markdownbeamer

Change Default Beamer slide size


In R markdown when using Beamer slides if you try to plot you need to specify the plot sizes (unlike in the reports) so that the plots fit on a page. This can often result in the plots appearing to be squashed together, as apposed to just a smaller version of the plot. Is there some method to change the default slide size to alleviate this problem?

I have tried

header-includes: 
    - \usepackage[papersize={25.6cm,19.2cm}]{geometry}

in the yaml header, and I get the error

! LaTeX Error: Missing \begin{document}.

Using R Markdown I shouldn't need to use this though.

A reproducible example is shown below

---
title: "Plots look bad"
author: "Beavis"
date: "`r format(Sys.time(), '%d/%m/%Y')`"
output: beamer_presentation
header-includes: 
   - \usepackage{float}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(results = 'hide')
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(cache=TRUE)
knitr::opts_chunk$set(fig.height=3.5)
```

# Introduction


```{r}
pca <- prcomp(iris[,1:4])
biplot(pca)
```

Here, you if you run this the second slide looks like this tin As you can see the plot is rubbish. What is the best way to avoid this problem?


Solution

  • The biplot function uses par(pty = "s") to force a square plot, so it's not going to fill a rectangular slide. You can make it look better by asking the fig.height to be bigger, but then it will overflow the bottom of the slide. To prevent this, you can set both fig.height to a large number, and out.height (which will be a LaTeX measurement) to something that will fit on a slide. For example, using this chunk

    ```{r fig.height=10, out.height="0.8\\textheight"}
    pca <- prcomp(iris[,1:4])
    biplot(pca)
    ```
    

    I see this output:

    screenshot

    I'd recommend a smaller fig.height, but your preference may be different.

    Both fig.height and out.height could be specified using knitr::opts_chunk$set as defaults for all slides if you want.

    You could also specify out.width="\\textwidth" for a really ugly stretched plot that fills the slide, but I wouldn't recommend it.