how to set figure size in rmd file

oluwafemi oloye - updated on March 27, 2024 . 3 Min Read

In an R Markdown (Rmd) file, you can control the size of figures (plots or images) using chunk options. Here are a few ways to set the figure size:

Using fig.width and fig.height:

In your R code chunk, specify the fig.width and fig.height options (in inches) to set the physical size of the plot.

                      
```{r, fig.width=8, fig.height=6}
  # Your plotting code here
```
                
                  
This will create a plot with a width of 8 inches and a height of 6 inches.

Using fig.dim

Alternatively, you can use the fig.dim option to specify both width and height as a numeric vector.

                      
```{r, fig.dim=c(8, 6)}
# Your plotting code here
```                        
                
                  

This is equivalent to setting fig.width = 8 and fig.height = 6..

Displaying Different Sizes in Output

You can choose to display a different size in the output using the out.width and out.height options. For instance, out.width = "50%" will display the plot at 50% of the container width..

Including External Images

If you want to include an external image (not generated from an R code chunk), you have two options:

Use Markdown syntax:

                    
  !A nice image.{width=50%}
  knitr::include_graphics("path/to/image.png")