【问题标题】:How to align table and plot in rmarkdown html_document如何在 rmarkdown html_document 中对齐表格和绘图
【发布时间】:2019-01-22 16:49:07
【问题描述】:

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

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

【问题讨论】:

标签: r ggplot2 r-markdown kable


【解决方案1】:

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

【讨论】:

  • 这帮助很大!顺便说一句,如果桌子比情节长得多,有没有办法让桌子更短,即以某种方式裁剪桌子?
  • 或者如果不可能,将两个图水平对齐?
  • 是否可以只在一个块中制作它?我想在一个循环中制作很多,但要做到这一点,我猜我需要将它放在一个单独的块中。
猜你喜欢
  • 1970-01-01
  • 2021-07-24
  • 2022-10-04
  • 2017-01-14
  • 2020-05-28
  • 1970-01-01
  • 2015-07-20
  • 2019-04-24
  • 1970-01-01
相关资源
最近更新 更多