[R] knitr: side-by-side figures in R markdown

Michael Friendly friendly at yorku.ca
Wed Jun 19 15:23:27 CEST 2013


I've read all the docs on knitr and some blogs on this topic, but can't 
figure out how
to produce side-by-side figures in R markdown, except by composing a 
single figure
in R with par(mfrow=c(1,2)).
I know this can be done easily with the LaTeX engine, but why not with 
HTML output?

A small test file is below.  If I were composing HTML directly, I would 
just wrap the images
in a table, e.g.,

<table>
<tr><td><img src="fig1.png"></td>
     <td><img src="fig2.png"></td>
</tr>
</table>

Is there someway to achieve this with knitr, perhaps a chunk hook?

--- test-figs.Rmd -----
Testing side-by-side figs
==========================

```{r knitr-setup, include=FALSE}
library(knitr)
library(vcd)
opts_chunk$set(out.extra='style="display:block; margin: auto"', 
fig.align="center", fig.width=4, fig.height=4)
opts_knit$set(progress = FALSE, verbose = TRUE)
```

This doesn't do what I want:
```{r chunck1, fig.show='hold'}
plot(1:10)
plot(10:1)
```

This does:
```{r chunck2, fig.show='hold', fig.width=8}
op <- par(mfrow=c(1,2))
plot(1:10)
plot(10:1)
par(op)
```

What to do with grid-based graphics (requires messing with viewports):

```{r chunck3, fig.width=6}
data(Titanic, package="datasets")
mosaic(Titanic)
mosaic(Titanic, shade=TRUE)
```

-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list