Tables and Figures side-by-side in Knitr or RMarkdown Beamer
Asked Answered
R

4

15

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.

I am not using Kable or xtable since I get more control over the tables with 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

Rubbing answered 17/1, 2015 at 13:43 Comment(0)
C
22

As I've already answered the similar question like this, I repeat my answer in which I use ::: notation, adding the codes to create the output you may want.

You can use fenced_divs notation or ::: to create columns or `Two Content layout'. See also this page to know more about the notation.

## 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...
```
::::

:::

Since pandoc 2+, which supports the notation, was implemented in RStudio v1.2+, you may need to install RStudio v1.2+ first. The installation is easy enough (at least in my case); just download and install RStudio v1.2+. In the way of installation, the former version of RStudio on your computer will be replaced with the new one without uninstalling it manually.

The following figure is an example which you have if you implement the notation.

enter image description here

The MWE code which produced the slide above is here, too:

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

::::

:::
Copulate answered 4/1, 2019 at 15:26 Comment(3)
I don't understand why this answer is not more upvoted. It's the most general one and really solves it neatly.Hidebound
This approach is indeed very neat. As per this SO post, more than 2 columns are feasible. Interestingly the alignment of the content in the columns switches from top-alignment o center-alignment.Calley
Posed a question regarding how to adjust the vertical alignment in this approach for multiple columns.Calley
R
4

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.

Create mystyle.tex and write there:

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

In the Rmd file use these new definitions

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

enter image description here

Rivulet answered 21/11, 2017 at 8:47 Comment(1)
It works but after you use the \begincols ones you have to used it for the rest of the slides (overthought you just need one column) do you know a way around it?Cyler
C
1

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

  • 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.)
  • 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.)
  • 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.
  • 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).

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

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

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.

Charleycharlie answered 18/1, 2015 at 0:7 Comment(0)
C
0

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

Curassow answered 17/1, 2015 at 16:52 Comment(2)
Thanks Jthorpe, but that does not solve the problem. It does move the figure to the right but it is still a level below the table, no matter how much I resize either the figure or table.Rubbing
Perhaps the answer at the bottom of this page will help.Curassow

© 2022 - 2024 — McMap. All rights reserved.