shadowtalker
shadowtalker

Reputation: 13823

Can't replicate MWE for side-by-side plots in knitr / R Markdown (HTML output)

I found this MWE for side-by-side plots in Knitr+Latex, and I attempted to convert it to R Markdown using HTML output. This is what I tried:

```{r, fig.show='hold', fig.width=3, fig.height=2.5, out.width=".49\\textwidth"}
par(mar = c(4, 4, .1, .1), cex.lab = .95, cex.axis = .9, mgp = c(2, .7, 0), tcl = -.3)
plot(cars)
boxplot(cars$dist,xlab='dist')
```

However it seems to generate nothing at all. I'm having the same problem trying to replicate this other MWE, too.

Is there something wrong in my setup? How can I get this to work with HTML and not just PDF?

Edit: using mfrow or otherwise messing with the graphics device itself isn't an option because the plotting function I'm using (filled.contour) unfortunately takes over layout.

Upvotes: 0

Views: 331

Answers (2)

shadowtalker
shadowtalker

Reputation: 13823

I figured out the problem: I had fig.align='center' enabled. Apparently this conflicts somehow with the ability to place two plots on the same line.

Upvotes: 1

Ben Bolker
Ben Bolker

Reputation: 226047

The out.width directive is screwing things up. When you're not in LaTeX output mode, out.width="0.49\\textwidth" is meaningless ... (The other MWE you're referring to is also LaTeX style -- I don't know what you did to adapt it to HTML ...)

writeLines("
```{r, fig.show='hold', fig.width=3, fig.height=2.5}
    par(mar = c(4, 4, .1, .1), cex.lab = .95,
        cex.axis = .9, mgp = c(2, .7, 0), tcl = -.3)
    plot(cars)
    boxplot(cars$dist,xlab='dist')
```
",con="figtest.rmd")
rmarkdown::render("figtest.rmd")
browseURL("figtest.html")

seems to work fine.

If you need finer control of placement, you'll probably need to embed some HTML directives -- e.g.

<table><tr><td>
## chunk with first figure code
</td><td>
## chunk with second figure code
</td></tr></table>

Upvotes: 1

Related Questions