如何为 PDF 报告修复 ggplot 中的方形?

How to fix square shaped in ggplot for an PDF report?

我想在 RMarkdown 中修复我的绘图(通过使用 RStudio),以便它在 PDF 报告中呈现为正方形。

我的 RMarkdown 脚本:

---
header-includes: \usepackage{graphicx}
output: 
    pdf_document:
        keep_tex: true
---

```{r results='hide', message=FALSE, warning=FALSE, echo=FALSE}
library(ggplot2)

minX <- min(iris$Sepal.Length) # 4.3
maxX <- max(iris$Sepal.Length) # 7.9
minY <- min(iris$Sepal.Width)  # 2.0
maxY <- max(iris$Sepal.Width)  # 4.4

# 7.9 - 4.3 = 3.6. Extend the y range by 0.5 * (3.6 - 2.4) = 0.6

minY <- minY - 0.6
maxY <- maxY + 0.6

scatter <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) 
scatter <- scatter + geom_point()
scatter <- scatter + xlim(minX, maxX)
scatter <- scatter + ylim(minY, maxY)
#scatter <- scatter + coord_fixed()
scatter
```

我的PDF报告是这样的。这显然不是正方形:

如果我在我的 R 脚本中取消注释 coord_fixed() 行,我会得到这个:

现在,这是一个正方形,但它浪费了水平方向 space,而且它非常小。如何正确操作?

你可以告诉 knitr 做更大的数字,例如

```{r width=7,out.width="7in"}
 ...
```