Ben
Ben

Reputation: 21655

How to align table and plot in rmarkdown html_document

How can I align a kable table to be adjacent to a ggplot2 plot in an rmarkdown html_document?

Foo.Rmd

---
title: "Foo"
output: html_document
---

```{r setup, include=FALSE}
library(ggplot2)
library(knitr)
library(kableExtra)
```

# Table next to plot
```{r echo = FALSE}
kable(head(iris)) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
```

enter image description here

I tried following the solution here but to no avail.

Upvotes: 2

Views: 6195

Answers (1)

bdemarest
bdemarest

Reputation: 14667

A great solution to this issue is provided here by @ErrantBard: https://stackoverflow.com/a/40650190/645206. Please visit and upvote it! I am copying the solution in my answer to show how it works with your example, and to provide an image of the solution.

To better understand how these div tags work, learn more about the bootstrap library. Here is one good link: https://getbootstrap.com/docs/4.1/layout/grid/

---
title: "Foo"
output: html_document
---

```{r setup, include=FALSE}
library(ggplot2)
library(knitr)
library(kableExtra)
```

# Table next to plot
<div class = "row">
<div class = "col-md-6">
```{r echo=FALSE}
kable(head(iris)) %>%
  kable_styling(bootstrap_options = "striped", full_width = FALSE, position="left")
```
</div>

<div class = "col-md-6">
```{r echo=FALSE}
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
```
</div>
</div>

enter image description here

Upvotes: 14

Related Questions