Is there a way to make a ggplot graph take all the slide with {xaringan} presentation? I can do that if i export it to .png and place it as a background picture. But what about directly from chunk output?
2 Answers
A minimal example is at bottom.
Explanation: The default CSS styling for xaringan has 16px of padding on the top and bottom and 64 px on the sides. You can over-ride this by creating a CSS file (for example my_css.css
containing the following lines:
.remark-slide-content.full-slide-fig{
padding: 0px 0px 0px 0px;
width: 100%;}
Then reference the CSS file in your YAML. Be sure to specify that the slide ratio and the fig.asp()
chunk options are the same (for example, 16 x 9 for widescreen output). Evidently I am still missing another the xaringan default CSS containing padding, because if you use a colored background there will still be a small bar of background at the slide top...but you can avoid this by setting background color to white or transparent and using out.width="95%"
as a chunk option.
Finally, be sure to specify that a given slide uses the class full-slide-fig
.
---
title: "Full-slide figures with **xaringan**"
date: "`r Sys.Date()`"
output:
xaringan::moon_reader:
css: "my_css.css"
nature:
ratio: "16:9"
---
class: full-slide-fig
```{r setup, include = FALSE}
knitr::opts_chunk$set(
fig.asp = 9/16,
fig.align = 'center',
echo = F,
out.width = "95%",
dpi= 300
)
library(ggplot2)
```
```{r}
ggplot(data = mtcars, mapping = aes(disp, mpg))+
geom_point()
```
---
R-markdown is in it's essence just a method for writing markdown documents (such as html, pdf and so forth). In essence this allows us to use standard markdown, html and LaTeX to solve these types of problems.
As described in Yihui's book rmarkdown-cookbook we can use this to our advantage when it comes to placement of figures. As such one method is to simply save the image and then display it using standard latex commands
```{r image_save, include = FALSE}
library(ggplot2)
p <- ggplot(pressure, aes(x = temperature, y = pressure)) + geom_point()
ggsave('myimage.png', p, dpi = 300) #change dpi for better resolution. 300 is standard
```
\begin{figure}[!p]
\includegraphics{myimage.png}
\caption{some latex caption, because someone may prefer this}
\label{reference_label:1}
\end{figure}
Strictly speaking this does not guarantee that the plot is on it's own page, as other images may be included on the same page. For a more strict ruling one could include a file in the header to include a different package or overwrite the underlying code for figure placement such as suggested in answers to this question.
Yihui also explains how one can add code to the header in the same book.
output:
pdf_document:
includes:
in_header: "preamble.tex"
The preamble could for example include only
% Contents of preamble.tex
\makeatletter
\@fpsep\textheight
\makeatother
which is the first suggested answer in the linked thread. Note that if the one includes a latex package, the markdown compiler might not recognize this and latex might need to be written in a latex chunk as described by Yihui in chapter 6.11.
I was certain a similar result could be obtained using only knitr::opts_chunk$set(fig.pos = '!p')
but this did not seem to give the desired result. I am also certain that one could do something similar using html, but I am no html expert.
Minimal reproducible example
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Including Plots
Some more text
```{R, include = FALSE}
library(ggplot2)
p <- ggplot(pressure, aes(x = temperature, y = pressure)) + geom_point()
ggsave('myimage.png', p)
```
\begin{figure}[!p]
\includegraphics{myimage.png}
\caption{some latex caption, because someone may prefer this}
\label{reference_label:1} %Label which could be referenced somewhere else in the document.
\end{figure}
text after latex figure
# Random text following the graphis
Here you can place even more information! It is still the same page!