I would like to show two images stacked horizontally in a R Markdown report, but, no matter how much I downsize them, R Markdown keeps stacking them horizontally. Any suggestions? Ideally, the solution should work independently of the output, and without having to write CSS code (CSS! ). Realistically, a solution which at least works with HTML output will still be ok. Thanks!
Sample .Rmd code:
When you do a code chunk that produces a graphic, the way text or other images wrap around is, indeed, controlled by the CSS. So to make one code block produce parallel images you'd have to hack the CSS (god help you).
However, if we turn the images into grobs in a ggplot graphic then we can effectively make one image out of them. This depends on having the magick package installed:
Yes to this. Also, I'm sure there is a way to work work ggplot's Grobs, but they scare the living hell out of me, so...
Finally, I have no knowledge of CSS, but I remember I was once able to stack 2-3 images horizontally without any problems by simply copying some CSS bit from SO, it was not painful at all.
I can't find that report anymore, but I know if I can do it - you can do it
You can also just use the chunk parameters fig.show = "hold", out.width = "50%" (assuming you want each to be half the width of the page). For example in this post, I have the code chunk:
This works! And it's also pretty simple. Do you know how can I specify the image size? Since it's not in a code chunk, I can't use chunk options such as e.g. out.width and out.height.
this also works! It requires installing magick, which is not exactly immediate under Ubuntu (I decided to install Ubuntu on my home laptop for a variety of reasons) but I managed to do it. Being a code chunk, I can fiddle with sizes easily. BTW, it would work the same if I used patchwork in place of cowplot, right? I like the patchwork API more...
This might be better broken off into a separate topic, but it seems like a lot of people are intimidated by slide theme customisation using CSS. I wonder if we could jam SASS into the xaringan build process and then have YAML parameters appended to or processed alongside the SASS.
It would mean that you could customise the theme directly from the YAML, and using SASS variables users would only need to make changes to a few variables set up by the theme author...
Yes! Markdown is very simple to use and to learn. If you don't really need to run R code to generate images, I'd say stick with Markdown! (And my hope is that @yihui would suggest the same)
As for your resizing question, you can add a tiny bit of CSS (it is not painful at all!). Something like this:
## Show images
![](doggy.jpeg){width=50%} ![](kitty.jpeg){width=50%}
You can also specify px instead of %. E.g.
## Show images
![](doggy.jpeg){width=200px} ![](kitty.jpeg){width=300px}
The only advantage, IMO, of using the code block and knitr::include_graphics is you get figure captions and references. So you can more easily link to the image later, if needed. But if you don't need figure captions and dynamic links to the figures, then I'm with @taras: just use markdown.
Actually, Yihui Xie suggests using knitr::include_graphics since it's more portable:
Also, it appears that Yihui suggested using knitr::include_graphics + fig.show = "hold" to solve a problem very similar to mine (which I could have never googled, given the title)
But here captions are instrumental to the answer, so it could also be that he suggested it because of what you said ( knitr::include_graphics in code chunk I can reference the images)