Add Image to Plot in R (Example) | Draw Picture on Top of Graph Using ggplot2 & patchwork Packages

 

In this tutorial, I’ll show how to add a picture on top of a ggplot2 plot in the R programming language.

The article consists of this content:

Let’s get started.

 

Example Data, Add-On Packages & Default Graphic

First, let’s create some example data in R:

data <- data.frame(x = 1:6,         # Creating example data
                   y = 10:5)
data                                # Printing example data
#   x  y
# 1 1 10
# 2 2  9
# 3 3  8
# 4 4  7
# 5 5  6
# 6 6  5

Have a look at the previous output of the RStudio console. It shows that our example data has six rows and two numeric columns x and y.

We need to install and load the ggplot2 package, if we want to use the functions that are contained in the package:

install.packages("ggplot2")         # Install ggplot2 package
library("ggplot2")                  # Load ggplot2 package

Next, we can draw our data in a basic ggplot2 without an image:

ggp <- ggplot(data, aes(x, y)) +    # Create ggplot2 plot
  geom_point()
ggp                                 # Draw ggplot2 plot

 

r graph figure 1 add image

 

Figure 1 shows the output of the previous syntax – A ggplot2 scatterplot with default specifications and without showing an image on top.

Let’s add an image…

 

Example: Add Image to Plot Using inset_element() Function of patchwork Package

In this example, I’ll show how to draw a ggplot2 plot with an image on top.

We first need to load an image to R using the png package. For this, we have to install and load the png package first:

install.packages("png")             # Install png package
library("png")                      # Load png package

Now, we can use the readPNG function to load an image that we have stored on our computer:

my_image <- readPNG("C:/Users/Joach/Desktop/r-logo.png", native = TRUE)

If we want to add this image on top of our ggplot2 plot, we have to install and load the patchwork package as well:

install.packages("patchwork")       # Install patchwork package
library("patchwork")                # Load patchwork

Now, we can use the inset_element function to add our picture to our ggplot2 graphic:

ggp_image <- ggp +                  # Combine plot & image
  inset_element(p = my_image,
                left = 0.5,
                bottom = 0.55,
                right = 0.95,
                top = 0.95)
ggp_image                           # Draw combined plot

 

r graph figure 2 add image

 

The output of the previous R syntax is shown in Figure 2: A ggplot2 plot and an image on top.

 

Video & Further Resources

Do you need further information on the R syntax of this post? Then I can recommend to watch the following video of my YouTube channel. I show the R programming codes of this article in the video.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you may have a look at the other articles which I have published on my website. You can find some articles below:

 

In summary: In this tutorial you learned how to combine plots and images in the same graph in the R programming language. In case you have additional questions, please let me know in the comments below.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top