Here, you will learn different methods to put images and adjust them in R Markdown. Having a background in \(\LaTeX{}\) and HTML is so beneficial, but not a must here. I tried to give very basic instructions so even the beginners will follow and run the commands. If you have any questions, please leave a comment and I will answer. If necessary, we can make some edits on this page.
A very easy method is to use this command: ! [Caption] (Directory of the image in your local drive without the "C:" at the very first of the address). For instance, if I have an image in my Downloads folder, I give the address like this (without opening any chunks): ! [Patrick is astonished] (/Users/abolfazl/Downloads/patrick.jpg).
An image should be uploaded here:
It is very important to adjust the figures. Above patrick is centered using the <center> and </center> html commands respectively before and after the ! [] (directory of the image) command.
load.image function of the imager package
Another approach is to use the function load.image which is called from imager library. If you don’t have this library installed, use install.packages("imager"). If you want to check whether a package is already installed or not, we open a new chunk using Ctrl+alt+i [when you use this keyboard command, a chunk starting with ```{r} and ending with ``` will be opened], and run the following:
is_already_installed <- require(imager)
is_already_installed
## [1] TRUE
TRUE means it is already installed, and FALSE means you need to install it using the command already mentioned. Using the command update.packages(), all of the existing packages will be updated. Updating packages from time to time is necessary.
Back to our topic, using load.image, the following chunk will result in:
library(imager)
im = load.image("C:/Users/armin/Downloads/margotrobbie.jpg")
plot(im)
The image above is shown using load.image. The dimensions of the image is automatically shown here. This beautiful image of Margot Robbie has 1920 pixels width, and 1200 pixels height. In this method, you can change the pixels and make it blurry as follows. In fact, when using the following command, you can stretch the image ten times in all directions:
changed_size <- resize(im,round(width(im)/10),round(height(im)/10))
plot(changed_size)
Knitr::include_graphics() .
A full width image, without showing the dimensions, starting the chunk with ```{r, out.width="100%"}:
knitr::include_graphics("C:/Users/armin/Downloads/margotrobbie.jpg")
Above image is shown in full width, we can manipulate the width by the command out.width, or out.height. For instance, if we write ```{r, out.width="25%"}, the resulting quarter-width image would be like:
A very important topic is merging figures in Markdown. There are different approaches. We usually like to see the figures aligned either vertically or horizontally. Here, both of these methods are demonstrated.
gridExtra package
Using either rasterGrob(readJPEG()) or rasterGrob(readPNG()) is also another method. rasterGrob is called from the grid library. We can put two images next to each other using grid.arrange command from gridExtra library. This method requires calling grid, jpeg or png, and gridExtra libraries. We can call libraries using require or library commands.
library(grid)
library(jpeg)
library(png)
require(gridExtra)
pre_fig <- rasterGrob(readJPEG("C:/Users/armin/Downloads/margotrobbie.jpg"), interpolate=TRUE)
post_fig <- rasterGrob(readJPEG("C:/Users/armin/Downloads/pat.jpg"), interpolate=TRUE)
# grid.arrange(pre_fig, post_fig, ncol=2)
grid.arrange(pre_fig, post_fig, ncol=2)
1.1
Using the command mentioned in 1.1 two times exactly after each other, makes the figures aligned vertically. To be simpler, we run this (without opening any chunks): <center>! [] (/Users/Abolfazl/Downloads/patrick) ! [](/Users/Abolfazl/Downloads/margot_robbie) </center>. In this method, both of the figures are going to be aligned vertically.
knitr
library(knitr)
opts_chunk$set(echo = FALSE,
# out.width = "75%",
out.width = "100%",
fig.align = "center")
The main part of this approach is to use include_graphics as follows:
include_graphics(c("C:/Users/armin/Downloads/margotrobbie.jpg", "C:/Users/armin/Downloads/pat.jpg"))
We can also align the figures horizontally as follows. However, the height is intentionally set to 100 pixels using out.height="100px". out.width can also be used.
knitr::include_graphics("C:/Users/armin/Downloads/margotrobbie.jpg")
knitr::include_graphics("C:/Users/armin/Downloads/pat.jpg")
The height and width can be modified:
The images used in this page are not mine, patrick’s image was taken from Reddit. The ravishing Margot pic was not also taken by me!
Thanks for your attention.