首页 > 解决方案 > 如何在 rmarkdown html_document 中对齐表格和绘图

问题描述

如何将 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()
```

在此处输入图像描述

我尝试按照此处的解决方案进行操作,但无济于事。

标签: rggplot2r-markdownkable

解决方案


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

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

在此处输入图像描述


推荐阅读