在Knitr或RMarkdown Beamer中并排放置的表格和图形

时间:2021-03-04 06:13:02

I am trying to create a Beamer Presentation slide in RMarkdown / Knitr . In the slide I would like to have a table and a figure placed Side-by-side , and then some more text underneath. I can only get as far as my attempt as shown in the code. I would like to have the density plot placed, next to the Hmisc Table.

我想在RMarkdown / Knitr中创建一个Beamer Presentation幻灯片。在幻灯片中,我希望有一个桌子和一个并排放置的图形,然后是下面的一些文字。我只能在代码中显示我的尝试。我希望在Hmisc表旁边放置密度图。

I am not using Kable or xtable since I get more control over the tables with Hmisc.

我没有使用Kable或xtable,因为我可以通过Hmisc更好地控制表格。

Also, How can I adjust the text characteristics (font-size, type, color) in the individual slides?

另外,如何调整各个幻灯片中的文本特征(字体大小,类型,颜色)?

---
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

Thanks

3 个解决方案

#1


2  

There have been issue having two column layout in beamer presentation. But in the same post there is workaround:

在投影仪演示中存在两列布局的问题。但在同一篇文章中有解决方法:

In short: Error is related to pandoc conversion engine, which treats everything between \begin{...} and \end{...} as TeX. It can be avoided by giving new definition for begin{column} and end{column} in yaml header.

简而言之:错误与pandoc转换引擎有关,它将\ begin {...}和\ end {...}之间的所有内容视为TeX。通过为yaml标头中的begin {column}和end {column}提供新定义,可以避免这种情况。

Create mystyle.tex and write there:

创建mystyle.tex并在那里写:

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

In the Rmd file use these new definitions

在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

And you get:

你得到:

在Knitr或RMarkdown Beamer中并排放置的表格和图形

#2


1  

Consider using a two column layout, like you would have to do if you were doing this directly in Beamer. See for example:

考虑使用两列布局,就像您在Beamer中直接执行此操作一样。参见例如:

  • this question on doing this with the tools available with RStudio. (Please note that this is one area where RStudio and the RMarkdown package have evolved a lot recently and the question is somewhat dated, but it does hint at the features now available.)
  • 有关使用RStudio提供的工具执行此操作的问题。 (请注意,这是RStudio和RMarkdown软件包最近发展很多的一个领域,这个问题有点过时了,但它确实暗示了现有的功能。)

  • this question for a solution with inline LaTeX and Pandoc. (This will also work with RStudio as the newer releases use a bundled copy of pandoc as the Markdown engine.)
  • 这个问题适用于内联LaTeX和Pandoc的解决方案。 (这也适用于RStudio,因为较新的版本使用pandoc的捆绑副本作为Markdown引擎。)

  • this post on the pandoc mailing list discussing how to include Markdown inside of your LaTeX blocks, e.g. the Beamer commands/environments for columns.
  • pandoc邮件列表中的这篇文章讨论了如何在LaTeX块中包含Markdown,例如:列的Beamer命令/环境。

  • this question on TeX Stack Exchange could help you, but you would need to adapt it a bit for RMarkdown (the question uses the Sweave-style syntax for embedding R into LaTeX with knitr).
  • TeX Stack Exchange上的这个问题可以帮到你,但你需要为RMarkdown调整一下(这个问题使用Sweave风格的语法将R嵌入到LaTeX中,带有knitr)。

The basic idea for your problem would be a two-column layout for the upper portion of the slide, and a one-column layout for the bottom. You then put the individual R code blocks into their own column. (You may need to play with vertical spacing if the two figures differ in size.)

您的问题的基本思想是幻灯片上部的两列布局和底部的单列布局。然后,将各个R代码块放入它们自己的列中。 (如果两个数字的大小不同,您可能需要使用垂直间距。)

The Rpres format is all-or-nothing on column layouts for a given slide (at least last time I checked), so that solution would be less than ideal when you want the bottom part of the slide to be a single 'column'.

Rpres格式在给定幻灯片的列布局上是全有或全无的(至少我上次检查过),因此当您希望幻灯片的底部为单个“列”时,该解决方案将不太理想。

Another solution would be combining the two figures into one and then displaying the merged figure. I'm not sure how you would do with a table and a graphic, but for two graphics, you could use the gridExtra package to place two lattice or ggplot2 (or even an unholy mixture of both) next to each other in a single grid and thus in a single, combined figure.

另一种解决方案是将两个图组合成一个然后显示合并的图。我不确定如何处理表格和图形,但是对于两个图形,您可以使用gridExtra包将两个格子或ggplot2(或者两者的非混合物)放在一个网格中彼此相邻因此,在一个单一的,组合的数字。

#3


0  

I think you want to set the chunk option fig.align=right as described here

我想你想设置chunk选项fig.align = right,如这里所述

#1


2  

There have been issue having two column layout in beamer presentation. But in the same post there is workaround:

在投影仪演示中存在两列布局的问题。但在同一篇文章中有解决方法:

In short: Error is related to pandoc conversion engine, which treats everything between \begin{...} and \end{...} as TeX. It can be avoided by giving new definition for begin{column} and end{column} in yaml header.

简而言之:错误与pandoc转换引擎有关,它将\ begin {...}和\ end {...}之间的所有内容视为TeX。通过为yaml标头中的begin {column}和end {column}提供新定义,可以避免这种情况。

Create mystyle.tex and write there:

创建mystyle.tex并在那里写:

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

In the Rmd file use these new definitions

在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

And you get:

你得到:

在Knitr或RMarkdown Beamer中并排放置的表格和图形

#2


1  

Consider using a two column layout, like you would have to do if you were doing this directly in Beamer. See for example:

考虑使用两列布局,就像您在Beamer中直接执行此操作一样。参见例如:

  • this question on doing this with the tools available with RStudio. (Please note that this is one area where RStudio and the RMarkdown package have evolved a lot recently and the question is somewhat dated, but it does hint at the features now available.)
  • 有关使用RStudio提供的工具执行此操作的问题。 (请注意,这是RStudio和RMarkdown软件包最近发展很多的一个领域,这个问题有点过时了,但它确实暗示了现有的功能。)

  • this question for a solution with inline LaTeX and Pandoc. (This will also work with RStudio as the newer releases use a bundled copy of pandoc as the Markdown engine.)
  • 这个问题适用于内联LaTeX和Pandoc的解决方案。 (这也适用于RStudio,因为较新的版本使用pandoc的捆绑副本作为Markdown引擎。)

  • this post on the pandoc mailing list discussing how to include Markdown inside of your LaTeX blocks, e.g. the Beamer commands/environments for columns.
  • pandoc邮件列表中的这篇文章讨论了如何在LaTeX块中包含Markdown,例如:列的Beamer命令/环境。

  • this question on TeX Stack Exchange could help you, but you would need to adapt it a bit for RMarkdown (the question uses the Sweave-style syntax for embedding R into LaTeX with knitr).
  • TeX Stack Exchange上的这个问题可以帮到你,但你需要为RMarkdown调整一下(这个问题使用Sweave风格的语法将R嵌入到LaTeX中,带有knitr)。

The basic idea for your problem would be a two-column layout for the upper portion of the slide, and a one-column layout for the bottom. You then put the individual R code blocks into their own column. (You may need to play with vertical spacing if the two figures differ in size.)

您的问题的基本思想是幻灯片上部的两列布局和底部的单列布局。然后,将各个R代码块放入它们自己的列中。 (如果两个数字的大小不同,您可能需要使用垂直间距。)

The Rpres format is all-or-nothing on column layouts for a given slide (at least last time I checked), so that solution would be less than ideal when you want the bottom part of the slide to be a single 'column'.

Rpres格式在给定幻灯片的列布局上是全有或全无的(至少我上次检查过),因此当您希望幻灯片的底部为单个“列”时,该解决方案将不太理想。

Another solution would be combining the two figures into one and then displaying the merged figure. I'm not sure how you would do with a table and a graphic, but for two graphics, you could use the gridExtra package to place two lattice or ggplot2 (or even an unholy mixture of both) next to each other in a single grid and thus in a single, combined figure.

另一种解决方案是将两个图组合成一个然后显示合并的图。我不确定如何处理表格和图形,但是对于两个图形,您可以使用gridExtra包将两个格子或ggplot2(或者两者的非混合物)放在一个网格中彼此相邻因此,在一个单一的,组合的数字。

#3


0  

I think you want to set the chunk option fig.align=right as described here

我想你想设置chunk选项fig.align = right,如这里所述