【问题标题】:Making full-slide sized ggplot2 output {Xaringan}制作全幻灯片大小的 ggplot2 输出 {Xaringan}
【发布时间】:2020-12-04 19:46:52
【问题描述】:

有没有办法用 {xaringan} 演示使 ggplot 图表占据所有幻灯片?如果我将它导出为 .png 并将其放置为背景图片,我可以做到这一点。但是直接从块输出呢?

【问题讨论】:

    标签: r ggplot2 r-markdown xaringan


    【解决方案1】:

    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 的答案中建议的。
    易辉也在同一本书中对how one can add code to the header进行了解释。

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

    例如,序言可以只包括

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

    这是链接线程中的第一个建议答案。请注意,如果其中包含一个乳胶包,降价编译器可能无法识别这一点,乳胶可能需要写入一个乳胶块,如 Yihui 在chapter 6.11 中所述。

    我确信只使用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!
    

    【讨论】:

    • 但是这个解决方案正是 Alberson 试图避免的。将绘图保存到磁盘然后立即将其读回演示文稿是相当不雅的。此外,xaringan 是 HTML 输出,因此带有 output:pdf_document() 的 LaTeX 解决方案不相关
    【解决方案2】:

    一个最小的例子在底部。

    说明: 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()
    ```
    
    ---
    

    【讨论】:

      猜你喜欢
      • 2019-03-18
      • 2019-01-18
      • 2018-11-27
      • 2018-12-09
      • 1970-01-01
      • 2021-08-14
      • 2020-02-11
      • 2020-02-03
      • 1970-01-01
      相关资源
      最近更新 更多