18

I am trying to add and resize a local image to a .Rmd file in RStudio that will produce a pdf. I can add the file easily with

![My caption.](path/file.png)

but I have not figured out how to control the size of the image. I tried HTML code with a width attribute, but the image would not appear (I think this only works if outputting to HTML).

<img src="path/file.png" width="200px" />

I could not get this idea to work:

![My caption.](path/file.png =250x)

Is there a way to modify the Rmarkdown script to modify the size of the local image with only RMarkdown and base R?

There is a suggestion to use the png and grid packages, but I am limited to base R for my problem. For other users, however, I think this looks like a good solution.

Community
  • 1
  • 1
Eric Green
  • 7,385
  • 11
  • 56
  • 102
  • possible dups: http://stackoverflow.com/questions/15625990/how-to-set-size-for-local-image-using-knitr-for-markdown and http://stackoverflow.com/questions/14675913/how-to-change-image-size-markdown – EDi Jan 19 '15 at 22:09
  • @Edi, thanks. I clarified that I am looking for a base R approach. – Eric Green Jan 20 '15 at 00:17
  • 4
    You can use raw latex to include a figure in pdf_output: `\includegraphics[width=250pt]{path/file.png}` – tmpname12345 Jan 21 '15 at 19:16
  • @tmpname12345, you're right. i did not even think to try that. – Eric Green Jan 22 '15 at 00:14

4 Answers4

10

You can also specify the size of the image like so:

![](filepath\file.jpg){ width=50% }

The width and height attributes on images are treated specially. When used without a unit, the unit is assumed to be pixels. However, any of the following unit identifiers can be used: px, cm, mm, in, inch and %. There must not be any spaces between the number and the unit.

Source: Pandoc's RMarkdown Documentation - Images

Johnny
  • 751
  • 12
  • 24
  • 2
    I think this answer is the simplest solution. But it works only in RMarkdown (not in Markdown). – petzi Sep 19 '17 at 17:30
8

From @tmpname12345

You can use raw latex to include a figure in pdf_output: \includegraphics[width=250pt]{path/file.png}

Eric Green
  • 7,385
  • 11
  • 56
  • 102
3

In case anyone arrives here from google looking to insert an image into an RMarkdown html_document:

Insert directly

This method is arguably the easiest to change size

<img src="mypic.png" alt="drawing" width="200" height="50"/>

Another way

Note you can mix measurements like so: height="200" width=60%

![some caption text here](mypic.png){height="200" width=60% }

Insert via RMarkdown chunk

knitr::include_graphics("mypic.png")

Insert directly from URL

```{r echo=FALSE, out.width = '60%'}
image_url <- "http://www.example.com/mypic.png"
```
<center><img src="`r image_url`"></center>
stevec
  • 41,291
  • 27
  • 223
  • 311
1

A longer example with latex.

\begin{figure}
\includegraphics[width=250pt]{../images/pricePlot2006_1.5.png}
\caption{Prices through time.}\label{fig:1}
\end{figure}

Other figures created in the .Rmd are numbered automatically.

```{r namedBlock, fig.cap = "Lots of cars."}
plot(mtcars)
```