Useful tricks when including images in Rmarkdown documents
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Rmarkdown is an enormously useful system for combining text, output and graphics generated by R into a single document. Images, in particular, are a powerful means of communication in a report, whether they be data visualizations, diagrams, or pictures. To maximize the power of those images, Zev Ross has created a comprehensive list of tips and tricks for working with images in R markdown documents. Those tips include:
- How to change the width and height of images created by R, and of imported image files.
- How to change the resolution of your images. (Usually not an issue for HTML output, where the resolution is implied by the figure height/width, but relevant for Word and PDF documents and when consuming output on Retina displays).
- How to automatically reduce the size of PNG images generated by R.
- How to insert multiple images (say, all images in a directory) into your document at once.
- How to apply CSS styles (like borders and background colors) to individual images (or R code or other Rmarkdown chunks, for that matter).
While Zev's guide focuses on manipulating images as generated by R, this might also be a good time to revisit these tips on making the graphics themselves as attractive and useful as possible. Although written many years ago, most of these tips are still relevant, in particular:
- If you plan to print (or these days, view on high-resolution displays), use PDF output
- Use PNG for R graphics output, and use a high-resolution if you plan to print. Don't ever use JPG for R graphics output.
- Think about the aspect ratio of your graphics.
- Remove unused whitespace around your graphics (more of an issue for base R grahics; ggplot2 handles this pretty well already).
You may also find the Rmarkdown cheat sheet helpful if you're working with Rmarkdown documents.
ZevRoss Technical Blog: Tips and tricks for working with images and figures in R Markdown documents
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.