簡體   English   中英

在Knitr或RMarkdown Beamer中並排放置的表格和圖形

[英]Tables and Figures side-by-side in Knitr or RMarkdown Beamer

我想在RMarkdown / Knitr中創建一個Beamer Presentation幻燈片。 在幻燈片中,我希望有一個桌子和一個並排放置的圖形,然后是下面的一些文字。 我只能在代碼中顯示我的嘗試。 我希望在Hmisc表旁邊放置密度圖。

我沒有使用Kable或xtable,因為我可以通過Hmisc更好地控制表格。

另外,如何調整各個幻燈片中的文本特征(字體大小,類型,顏色)?

---
title: "BeamerTest1"
subtitle: Beamer Subtitle
author: "Author"

output:
  beamer_presentation:
    theme: CambridgeUS
    colortheme: "beaver"
    fonttheme: "structurebold"
---

## Slide with Table, Figure and Text

My topic for this slide 

\scalebox{0.35}{
```{r hmisc-table, echo=FALSE, message=FALSE, results='asis'}
library(Hmisc)
latex(head(mtcars), file='', table.env=FALSE, center='none')
```
}


```{r, echo=FALSE, fig.show='hold', fig.height=1, fig.width=2.5}
library(ggplot2)
mt <- ggplot(mtcars, aes(mpg)) + geom_density(alpha=.2, fill="#FF6666") +
  theme(axis.title.x = element_text(size=10),axis.text.x  = element_text(size=8),
        axis.title.y = element_text(size=10),axis.text.y  = element_text(size=8))
mt
```

- Here is some Bullet Text
- And some more
    - Subtext
    - More Subtext

謝謝

在投影儀演示中存在兩列布局的問題 但在同一篇文章中有解決方法:

簡而言之:錯誤與pandoc轉換引擎有關,它將\\begin{...}\\end{...}視為TeX 通過為yaml標頭中的begin{column}end{column}提供新定義,可以避免這種情況。

創建mystyle.tex並在那里寫:

\def\begincols{\begin{columns}}
\def\begincol{\begin{column}}
\def\endcol{\end{column}}
\def\endcols{\end{columns}}

在Rmd文件中使用這些新定義

---
output:
  beamer_presentation:
    includes:
      in_header: mystyle.tex
---


Two Column Layout 
-------

\begincols
  \begincol{.48\textwidth}

This slide has two columns.

  \endcol
\begincol{.48\textwidth}

```{r}
#No error here i can run any r code
plot(cars)
```

  \endcol
\endcols

你得到:

在此輸入圖像描述

考慮使用兩列布局,就像您在Beamer中直接執行此操作一樣。 參見例如:

  • 有關使用RStudio提供的工具執行此操作的問題 (請注意,這是RStudio和RMarkdown軟件包最近發展很多的一個領域,問題有些過時,但它確實暗示了現有的功能。)
  • 這個問題適用於內聯LaTeX和Pandoc的解決方案。 (這也適用於RStudio,因為較新的版本使用pandoc的捆綁副本作為Markdown引擎。)
  • 這篇帖子在pandoc郵件列表中討論了如何在LaTeX塊中包含Markdown,例如列的Beamer命令/環境。
  • TeX Stack Exchange上的這個問題可以幫到你,但是你需要為RMarkdown調整一下(這個問題使用Sweave風格的語法將R嵌入到帶編織器的LaTeX中)。

您的問題的基本思想是幻燈片上部的兩列布局和底部的單列布局。 然后,將各個R代碼塊放入它們自己的列中。 (如果兩個數字大小不同,您可能需要使用垂直間距。)

Rpres格式在給定幻燈片的列布局上是全有或全無(至少在我上次檢查時),因此當您希望幻燈片的底部為單個“列”時,該解決方案將不太理想。

另一種解決方案是將兩個圖組合成一個然后顯示合並的圖。 我不確定如何使用表格和圖形,但對於兩個圖形,您可以使用gridExtra包將兩個latticeggplot2 (或者甚至是兩者的混合物)放在一個grid彼此相鄰因此,在一個單一的,組合的數字。

正如我已經回答了類似的問題這樣 ,我重申我的回答中,我使用:::符號,添加代碼來創建你可能需要的輸出。

您可以使用fenced_divs表示法或:::來創建列或“兩個內容布局”。 另請參閱此頁面以了解有關表示法的更多信息。

## Slide With Image Left

::: columns

:::: column
left
::::

:::: column
right

```{r your-chunk-name, echo=FALSE, fig.cap="your-caption-name"}
knitr::include_graphics("your/figure/path/to/the-image.pdf")

#The figure will appear on the right side of the slide...
```
::::

:::

由於支持符號的pandoc 2+是在RStudio v1.2+ ,因此您可能需要首先安裝RStudio v1.2+ 安裝很簡單(至少在我的情況下); 只需下載並安裝RStudio v1.2+ 在安裝過程中,計算機上的舊版RStudio將被替換為新版本,而無需手動卸載。

下圖是您實現表示法時的示例。

在此輸入圖像描述

制作上面幻燈片的MWE代碼也在這里:

---
title: "BeamerTest1"
subtitle: Beamer Subtitle
author: "Author"

output:
  beamer_presentation:
  theme: CambridgeUS
  colortheme: "beaver"
  fonttheme: "structurebold"
---

## Slide with Table, Figure and Text

::: columns

:::: column

My topic for this slide 

\scalebox{0.35}{
```{r hmisc-table, echo=FALSE, message=FALSE, results='asis'}
library(Hmisc)
latex(head(mtcars), file='', table.env=FALSE, center='none')
```
}


```{r, echo=FALSE, fig.show='hold', fig.height=1, fig.width=2.5}
library(ggplot2)
mt <- ggplot(mtcars, aes(mpg)) + geom_density(alpha=.2, fill="#FF6666") +
theme(axis.title.x = element_text(size=10),axis.text.x  = element_text(size=8),
axis.title.y = element_text(size=10),axis.text.y  = element_text(size=8))
mt
```

::::

:::: column

- Here is some Bullet Text
- And some more
    - Subtext
    - More Subtext

::::

:::

我想你想設置chunk選項fig.align=right ,如這里所述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM