给定以下rmarkdown文件:
```{r}
library(knitr)
```
```{r, out.width="100%"}
path <- "plotx.png"
knitr::include_graphics(path)
```
```{r, out.width="100%", out.height="150%"}
knitr::include_graphics(path)
```
Run Code Online (Sandbox Code Playgroud)
图形之间的大小没有差异,就好像图形大小选项没有任何影响一样。使用其他类似的选项,例如fig.width、fig.height,没有任何区别。
```{r}
library(knitr)
```
```{r, out.width="100%"}
path <- "plotx.png"
knitr::include_graphics(path)
```
```{r, out.width="100%", out.height="150%"}
knitr::include_graphics(path)
```
Run Code Online (Sandbox Code Playgroud)
对于 HTML 输出,设置out.width="100%"会将图像缩放为其父容器的 100% 宽度,而不是图像像素的 100%。从而保持纵横比,提供尽可能大的结果而不失真。设置out.width="100%" 并 out.height="150%"因此产生相同的结果。使用像素来获取精确尺寸(这可能会导致失真)。
对于 LaTeX 输出,无论使用像素还是 %,纵横比都会保持不变(这里唯一的区别是像素被视为像素,% 会转换为缩放因子 和\textheight)\linewidth。您可以使用 chunk 选项来防止这种情况out.extra='keepaspectratio=false'。
例子:
图片来自这里。
---
title: "Untitled"
output:
html_document: default
pdf_document: default
---
Run Code Online (Sandbox Code Playgroud)
LaTeX 和 HTML:宽度 = 100px,保持纵横比
```{r, out.width = "100px"}
tiger <- "tiger.png"
knitr::include_graphics(tiger)
```
Run Code Online (Sandbox Code Playgroud)

LaTeX:保留长宽比,HTML:宽度 = 700px,高度 = 200px不保留长宽比
```{r, out.height = "200px", out.width = "700px"}
knitr::include_graphics(tiger)
```
Run Code Online (Sandbox Code Playgroud)

(左侧为 LaTeX,右侧为 HTML。图像已缩放以适合并排。)
| 归档时间: |
|
| 查看次数: |
1838 次 |
| 最近记录: |