0

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.

Community
  • 1
  • 1
shadowtalker
  • 12,529
  • 3
  • 53
  • 96
  • I get both plots when I run your example, but the plots are one on top of the other. You neglected to include `fig.show="hold"` in your chunk parameters, which will give you the side-by-side plots. Another option for side-by-side plots would be `\`\`\`{r, fig.width=6, fig.height=2.5} par(mfrow=c(1,2)) plot(cars) boxplot(cars$dist,xlab='dist')\`\`\`` – eipi10 Jun 15 '16 at 19:31
  • @eipi10 my mistake, I do have `fig.show="hold"` in my code – shadowtalker Jun 15 '16 at 19:39
  • When I use your (now updated code) with `fig.show = "hold"` this is a perfect plot witz both plots side-by-side as pdf. As HTML I see only the code and no plot. – J_F Jun 15 '16 at 20:32
  • @J_F I also forgot to specify I'd like to use HTML output -_- – shadowtalker Jun 15 '16 at 21:03
  • In the RStudio viewer you see no plot, but when you open your *.html file in your output folder with a browser, you will see the two plots, but not side-by-side. – J_F Jun 15 '16 at 21:17

2 Answers2

1

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>
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks. I wonder if my issue is a quirk specifically with `filled.contour`, then, because your code does in fact work fine. But when I swap my plotting functions in, they are always on separate lines. If the solution isn't trivial I'll ask a separate question. – shadowtalker Jun 16 '16 at 03:33
1

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.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96