将 ggplot2 输出设置为全屏幕大小 {Xaringan}

4

有没有一种方法可以使ggplot图表在{xaringan}演示时占据整个幻灯片?如果我将其导出为.png文件并将其设置为背景图片,那么我可以实现这一点。但是直接从代码块输出怎么办?

2个回答

5
一个最简示例在底部。
说明:默认CSS样式的在顶部和底部具有16px的填充和64像素的侧面填充。您可以通过创建一个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()
```

---

你能否指明如何删除幻灯片顶部的小背景条,因为我已经尝试了几个小时,但仍然无法弄清楚。如果您想查看我的问题,这里是我在此处发布的类似问题的链接:https://stackoverflow.com/questions/76207721/xaringan-slides-made-with-xaringanthemer-does-not-show-the-plot-properly。提前感谢您的帮助。 - mzkrc

2

R-markdown本质上是一种编写Markdown文档(如html、pdf等)的方法。基本上,这使我们可以使用标准的markdown、html和LaTeX来解决这些类型的问题。

正如Yihui在他的书《rmarkdown-cookbook》中所述,当涉及到插图位置时,我们可以利用这一点。因此,一种方法是简单地保存图像,然后使用标准的LaTeX命令来显示它。

```{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}

严格来说,这并不保证图形在自己的页面上,因为其他图片可能会放在同一页上。为了更严格的规定,可以在页眉中包含一个文件,以包含不同的包或覆盖图形放置的底层代码,如此问题的答案所建议的那样。
Yihui 在同一本书中还解释了如何将代码添加到页眉

output:
  pdf_document:
    includes:
      in_header: "preamble.tex"

例如,序言可能仅包括以下内容:

% Contents of preamble.tex
\makeatletter
\@fpsep\textheight
\makeatother

这是链接主题中提供的第一个建议答案。请注意,如果其中包含LaTeX包,则markdown编译器可能无法识别此内容,需要像Yihui在第6.11章中所述那样在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, 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!

1
但这个解决方案恰恰是Alberson试图避免的。将图形保存到磁盘,然后立即将其读回演示文稿中相当不优雅。此外,xaringan是HTML输出,因此使用output:pdf_document()的LaTeX解决方案并不相关。 - Evan M

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接