在R Notebook中使用fig_height和fig_width设置图尺寸失效求助
Great question! This isn't a mistake on your part—it's a deliberate design difference between html_notebook and html_document in R Markdown. The fig_height and fig_width parameters in the YAML header don't work for html_notebook output, but they function as expected for html_document because of how each format handles rendering.
Why the difference?
html_document generates a static HTML file, and the YAML configuration parameters are passed directly to knitr to apply global settings to all code chunks. On the other hand, html_notebook is an interactive format built for real-time editing and preview in RStudio. Its rendering logic prioritizes per-chunk settings over global YAML parameters for figure dimensions, so those YAML entries are ignored.
Two ways to set figure dimensions in R Notebook
1. Per-chunk settings
Add fig.height and fig.width directly to individual R code chunks where you need to adjust figure size:
```{r fig.height=2, fig.width=2} plot(cars)
#### 2. Global default settings Set a global default for all code chunks using knitr's chunk options. Add this setup chunk at the very top of your notebook: ```r ```{r setup, include=FALSE} knitr::opts_chunk$set(fig.height = 2, fig.width = 2)
The `include=FALSE` ensures this chunk runs in the background without appearing in your notebook output. Either approach will give you the 2x2 figure dimensions you want in your R Notebook. 内容的提问来源于stack exchange,提问作者fallsit

