Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing images in RMarkdown

Tags:

r

r-markdown

I'm trying to convert a R markdown .Rmd document to .pdf. Unfortunately, the images are too large. Is there any way to change the size of the image? I Can't use html, this is markdown to pdf.

like image 256
IBTL Avatar asked Nov 12 '14 07:11

IBTL


People also ask

How do I resize an image in Markdown?

Show activity on this post. With certain Markdown implementations (including Mou and Marked 2 (only macOS)) you can append =WIDTHxHEIGHT after the URL of the graphic file to resize the image. Do not forget the space before the =.

How to add images to an R Markdown report?

You can add images to an R Markdown report using markdown syntax as follows: However, when you knit the report, R will only be able to find your image if you have placed it in the right place - RELATIVE to your .Rmd file.

How to resize an image in RMD without HTML?

For those intereseted in an rmarkdown and knitr solution. There are some ways to resize images in an .rmd file without the use of html: You can simply specify a width for an image by adding {width=123px}. Don't introduce whitespace in between the brackets:

Is it possible to use CSS in R Markdown?

CSS style: yes (with out.extra, example: out.extra='style="background-color: #9ecff7; padding:10px; display: inline-block;"' See blog post Tips and tricks for working with images and figures in R Markdown documents The behavior is the same as in Images in .md files.


2 Answers

Use this at the beginning of a chunk:

Decimals assigned to fig.height and fig.width are interpreted as inches. Other units of measure also allowed if explicit.

```{r, echo=FALSE, fig.height=2.7, fig.width=9}
#your R code here
```
like image 171
lgadar Avatar answered Nov 13 '22 10:11

lgadar


I found a comfortable solution by the combination of fig.height, fig.width, dpi and out.width.

You can set global parameters at the top by:

knitr::opts_chunk$set(out.width="400px", dpi=120)

You can overwrite these properties in any chunk, just set the parameters you need.

dpi increases the quality image, so you have to adjust by the other parameters.

out.width adjust the size once the image is created.

Decreasing values in fig.height and fig.width will cause the text/numbers to be bigger (same as reducing image window in Rstudio)

like image 26
Pablo Casas Avatar answered Nov 13 '22 11:11

Pablo Casas