Reputation: 2643
I have a local image that I would like to include in an .Rmd
file which I will then knit
and convert to HTML slides with Pandoc
. Per this post, this will insert the local image :

Is there a way to modify this code to also set the image size?
Upvotes: 187
Views: 166693
Reputation: 2949
Un updated answer: in knitr 1.17
you can simply use
{width=250px}
edit as per comment from @jsb
Note this works only without spaces, e.g. {width=250px} not {width = 250px}
Upvotes: 155
Reputation: 111
Another option that worked for me is playing with the dpi option of knitr::include_graphics()
like this:
```{r}
knitr::include_graphics("path/to/image.png", dpi = 100)
```
... which sure (unless you do the math) is trial and error compared to defining dimensions in the chunk, but maybe it will help somebody.
Upvotes: 8
Reputation: 12640
Here's some options that keep the file self-contained without retastering the image:
div
tags<div style="width:300px; height:200px">

</div>
---
title: test
output: html_document
css: test.css
---
## Page with an image {#myImagePage}

#myImagePage img {
width: 400px;
height: 200px;
}
If you have more than one image you might need to use the nth-child pseudo-selector for this second option.
Upvotes: 38
Reputation: 127
Had the same issue today and found another option with knitr 1.16
when knitting to PDF (which requires that you have pandoc installed):
{width=70%}
This method may require that you do a bit of trial and error to find the size that works for you. It is especially convenient because it makes putting two images side by side a prettier process. For example:
{width=70%}{width=30%}
You can get creative and stack a couple of these side by side and size them as you see fit. See https://rpubs.com/RatherBit/90926 for more ideas and examples.
Upvotes: 11
Reputation: 73
The knitr::include_graphics solution worked well for resizing the figures, but I was unable to figure out how to use it to produce side-by-side resized figures. I found this post useful for doing so.
Upvotes: 4
Reputation: 14997
The question is old, but still receives a lot of attention. As the existing answers are outdated, here a more up-to-date solution:
As of knitr
1.12, there is the function include_graphics
. From ?include_graphics
(emphasis mine):
The major advantage of using this function is that it is portable in the sense that it works for all document formats that
knitr
supports, so you do not need to think if you have to use, for example, LaTeX or Markdown syntax, to embed an external image. Chunk options related to graphics output that work for normal R plots also work for these images, such asout.width
andout.height
.
```{r, out.width = "400px"}
knitr::include_graphics("path/to/image.png")
```
Advantages:
To compose the path to a plot that is generated in a chunk (but not included), the chunk options opts_current$get("fig.path")
(path to figure directory) as well as opts_current$get("label")
(label of current chunk) may be useful. The following example uses fig.path
to include the second of two images which were generated (but not displayed) in the first chunk:
```{r generate_figures, fig.show = "hide"}
library(knitr)
plot(1:10, col = "green")
plot(1:10, col = "red")
```
```{r}
include_graphics(sprintf("%sgenerate_figures-2.png", opts_current$get("fig.path")))
```
The general pattern of figure paths is [fig.path]/[chunklabel]-[i].[ext]
, where chunklabel
is the label of the chunk where the plot has been generated, i
is the plot index (within this chunk) and ext
is the file extension (by default png
in RMarkdown documents).
Upvotes: 230
Reputation: 121608
You can also read the image using png
package for example and plot it like a regular plot using grid.raster
from the grid
package.
```{r fig.width=1, fig.height=10,echo=FALSE}
library(png)
library(grid)
img <- readPNG("path/to/your/image")
grid.raster(img)
```
With this method you have full control of the size of you image.
Upvotes: 112
Reputation: 1136
If you are converting to HTML, you can set the size of the image using HTML syntax using:
<img src="path/to/image" height="400px" width="300px" />
or whatever height and width you would want to give.
Upvotes: 21