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

Yihui Xie xie at yihui.name
Wed Jun 19 23:31:41 CEST 2013


You need to remove out.extra='style="display:block; margin: auto"'. In
CSS, display:block; means this element stands in its own line, and no
other elements can sit by its side. This is applied to individual
images, so the two images will not be arranged side by side.

But you will lose the center alignment without display:block. The
difficulty comes from the fact that Markdown images are not put into
div containers as in HTML.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 206-667-4385 Web: http://yihui.name
Fred Hutchinson Cancer Research Center, Seattle


On Wed, Jun 19, 2013 at 6:23 AM, Michael Friendly <friendly at yorku.ca> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list