


The fig.width argument has no effect on how external images are rendered. Image 1 output (width = 500px and height = 333.5px, 300dpi, 1.2mb on disk): The viewable size in our HTML document is ½ the size of the original image – the default for an external image.The R-generated figure however is output using the fig.width default of 7 inches. The external images are unaffected by the fig.width argument (which is set to 7 inches by default). This is the default for images using the include_graphics function – original px width * 50%. You'll see below that the default for images is to display them at ½ their original size – you will see below that this is due to the fig.retina = 2 setting (making the same image ½ the size doubles the resolution). How images and figures in the HTML document are affected by using defaults Second, although you can technically include an image in a markdown document using standard HTML image tags ( ), using include_graphics will respect image settings listed in the R chunks like out.width and out.height.Īs mentioned above, the figure is included by creating a new plot on the fly with the ggplot2 package. First, the function is document format agnostic – meaning it can work with LaTeX or Markdown documents. We are using include_graphics for two reasons. img1_path ) or using markdown directly ( !(imagepath)). The raw image on disk has a width of 1000px and height of 667px (300 dpi). Library(png) # For grabbing the dimensions of png files Start by loading the packages library(knitr) # For knitting document and include_graphics function For example library(jpeg) readJPEG(img1). NOTE: you can use the same process to examine jpegs, simply swap out png for jpeg. Use the attr function: get the dimensions of the image as well as some other potentially useful information (color type, dpi, etc).Use the dim function: get the dimensions of the image.There are two ways to grab the dimensions (height and width) of the image. The image was downloaded to our local drive from here and can be used under the Creative Commons CC0 license.Īs a starting point, we can compute the dimensions of our raw image using the readPNG function from the package png. Moving forward we're going to refer to our uploaded image as image and the R-generated plot as figure. The plot is created using the package ggplot2. In this post we'll work with a pre-existing image as well as a dynamically generated plot. Our examples: one pre-existing image and one dynamically generated plot More functionality from include_graphics.Bonus knitr and R markdown functionality.Optimize R-generated images with optipng or pngquant.The fig.retina argument is a resolution multiplier.Use dpi to change the resolution of images and figures.Arguments out.width and out.height apply to both existing images and R-generated figures.Use fig.width and fig.height for R-generated figures only.Default settings for including images and figures in R Markdown.Our examples: one pre-existing image and one dynamically generated plot.In this post, we report image dimensions as they appear at full size on a computer monitor for reference. NOTE 2: Images in the final HTML documents are responsive – meaning that their dimensions may change with changes to the browser view size.

NOTE 1: This post is focused on the production of HTML documents and some of our conclusions and recommendations may not apply if you're using R Markdown to create a LaTeX document, PDF or Word document. We assembled this blog post to help guide you through image processing decision-making as you construct your own R Markdown reports. R Markdown offers a wide range of functions and arguments for full control of image sizes but knowing how and when to use them can be daunting particularly given the differences in how external images are handled vs R-generated figures. R Markdown reports that are heavy on graphs and maps, though, can yield large HTML files that are not optimized for web viewing. Writing reports in R Markdown allows you to skip painful and error-prone copy-paste in favor of dynamically-generated reports written in R and markdown that are easily reproducible and updateable.
