I want to make a single figure in R with two plots in a markdown file with knitr. Normally, this is easy to do with layout(t(1:2)) or par(mfrow=c(1,2)). Can I do this with knitr, or will it always make two separate figures?
Here is a minimum working example which creates a file called ./junk.Rmd and ./junk.md in your working directory along with two files ./figure/junkislands1.png (which only includes the first plot) and ./figure/junkislands2.png (which includes both plots that I want).
require(knitr)
temp <- "```{r junkislands, fig.width=8, fig.height=5}
layout(t(1:2))
pie(islands)
barplot(islands)
```"
cat(temp, file="junk.Rmd")
knit("junk.Rmd", "junk.md")
The problem isn’t so much that it creates two .png files, but rather that the markdown file junk.md includes both of them.
When I make that markdown into html, it includes both .png files when I only want the one with both figures plotted.
Here is the file junk.md that is generated from knitr:
```r
par(mfrow = c(1, 2))
pie(islands)
```

```r
barplot(islands)
```

Have a look at http://yihui.name/knitr/options and specifically
fig.keep. I think you wantfig.keep = 'last'gives