如何在rmarkdown html_document中对齐表格和绘图

时间:2019-01-22 16:49:07

标签: r ggplot2 r-markdown kable

如何在rmarkdown html_document中对齐山墙表使其与ggplot2图相邻?

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

我尝试遵循解决方案here,但无济于事。

1 个答案:

答案 0 :(得分:4)

@ErrantBard在这里提供了一个很好的解决方案:https://stackoverflow.com/a/40650190/645206。请访问并投票! 我正在复制答案中的解决方案,以显示它如何与您的示例配合使用,并提供解决方案的图片。

要更好地了解这些div标签是如何工作的,请了解有关引导程序库的更多信息。这是一个很好的链接: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