问题描述
有没有一种方法可以使ggplot图以{xaringan}演示文稿的形式呈现所有幻灯片?如果我将其导出到.png并将其放置为背景图片,则可以这样做。但是直接从块输出中呢?
解决方法
一个最小的例子在底部。
说明:xaringan 的默认 CSS 样式的顶部和底部填充为 16 像素,两侧为 64 像素。您可以通过创建 CSS 文件(例如 my_css.css
包含以下行:
.remark-slide-content.full-slide-fig{
padding: 0px 0px 0px 0px;
width: 100%;}
然后在您的 YAML 中引用 CSS 文件。确保指定滑动比例和 fig.asp()
块选项相同(例如,宽屏输出为 16 x 9)。显然我仍然缺少另一个包含填充的 xaringan 默认 CSS,因为如果您使用彩色背景,幻灯片顶部仍然会有一个小背景栏……但是您可以通过设置来避免这种情况背景颜色为白色或透明,并使用 out.width="95%"
作为块选项。
最后,请务必指定给定幻灯片使用 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本质上仅是一种编写markdown文档(例如html,pdf等)的方法。从本质上讲,这使我们可以使用标准markdown,html和LaTeX解决此类问题。
如Yihui的书rmarkdown-cookbook中所述,在放置数字时,我们可以利用它来发挥优势。这样一种方法就是简单地保存图像,然后使用标准的乳胶命令将其显示
```{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}
严格来说,这并不表示该图在其自己的页面上,因此不能保证其在同一页面上显示,因为其他图像可能会包含在同一页面上。对于更严格的裁定,可以在标头中包含一个文件以包含其他程序包,或者覆盖用于图形放置的基础代码,例如在this question的答案中建议的。
Yihui在同一本书中也解释了how one can add code to the header。
output:
pdf_document:
includes:
in_header: "preamble.tex"
例如,序言可以仅包含
% Contents of preamble.tex
\makeatletter
\@fpsep\textheight
\makeatother
这是链接线程中的第一个建议答案。请注意,如果其中包含一个Latex包,则markdown编译器可能无法识别这一点,并且可能需要按照chapter 6.11中Yihui的描述,以Latex块的形式编写Latex。
我敢肯定,仅使用knitr::opts_chunk$set(fig.pos = '!p')
可以得到类似的结果,但这似乎并没有达到预期的结果。我也确信使用html可以做类似的事情,但是我不是html专家。
最小的可复制示例
---
title: "Untitled"
output: pdf_document
---
```{r setup,include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Including Plots
Some more text
```{R,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!