我正在使用 R Markdown 创建一个带有 PDF 输出的文档。
我的代码在“for”循环中创建了许多表格和图形,在同一代码块中。我见过的在 PDF 文档中创建换行符的所有技巧 - 前面的斜线和空格的各种组合和排列(例如,换行符、换行符、
、  等)都不起作用。因此,我决定走捷径,只增加 ggplot2 边距中的绘图空间,以在我的表格和图表之间创造更多空白的错觉。
- 理想情况下,我很想知道如何在单个代码块中正确插入空格(一行或多行),而不会出错。
- 但是,当我有一些背景颜色(第一个图)但没有 在背景颜色为白色时工作!!?伙计们,这真是奇怪的行为!
请参阅下面的屏幕截图和代码,其中包含两个相同的代码块 - 除了绘图的背景颜色 - 其中一个浅米色背景的绘图显示了表格和绘图标题之间所需的“空白”,以及以下情节(白色背景)不显示相同的“空白”。
---
title: "I need a line break"
output: pdf_document
---
```{r setup, include=FALSE}
library(ggplot2)
library(knitr)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE,
include = TRUE,
message = FALSE,
warning = FALSE,
error = FALSE)
```
```{r, results = "asis", fig.height = 2}
for (i in 1) {
mytable <- kable((iris[c(1,2),]), booktabs = TRUE)
print(mytable)
myplot <- ggplot(iris) +
labs(title = "Gorgeous plot") +
geom_point(aes(Sepal.Length, Sepal.Width)) +
theme_void() +
theme(plot.background = element_rect(fill = "#FFFEEB", color = "white"),
plot.margin = unit(c(1, 0, 0, 0), "cm"))
print(myplot)
}
```
```{r, results = "asis", fig.height = 2}
for (i in 1) {
mytable <- kable((iris[c(1,2),]), booktabs = TRUE)
print(mytable)
myplot <- ggplot(iris) +
labs(title = "Gorgeous plot") +
geom_point(aes(Sepal.Length, Sepal.Width)) +
theme_void() +
theme(plot.background = element_rect(fill = "white", color = "white"),
plot.margin = unit(c(1, 0, 0, 0), "cm"))
print(myplot)
}
```
最佳答案
你能用 cat 添加换行符来解决你的问题吗?例如
---
title: "I need a line break"
output: pdf_document
---
```{r setup, include=FALSE}
library(ggplot2)
library(knitr)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE,
include = TRUE,
message = FALSE,
warning = FALSE,
error = FALSE)
```
```{r, results = "asis", fig.height = 2}
for (i in 1:2) {
mytable <- kable((iris[c(1,i),]), booktabs = TRUE)
print(mytable)
cat("\\ ")
cat("\\linebreak")
cat("\\linebreak")
cat("\\linebreak")
myplot <- ggplot(iris) +
labs(title = "Gorgeous plot") +
geom_point(aes(Sepal.Length, Sepal.Width)) +
theme_void()
print(myplot)
cat("\\linebreak")
cat("\\linebreak")
}
```
(需要 cat("\\")
以便有一行结束)
关于r - R Markdown 中带有 pdf_document 输出的表格和绘图之间的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68261268/