How to align table and plot in rmarkdown html_document

青春壹個敷衍的年華 提交于 2021-02-08 05:01:54

问题


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()
```

I tried following the solution here but to no avail.


回答1:


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>



来源:https://stackoverflow.com/questions/54312894/how-to-align-table-and-plot-in-rmarkdown-html-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!