Add a background png image to ggplot2

Hey everybody,

this is just a short post but I found it very useful. I want to show you how to add images as a background to your ggplot2 plots.

To do so we need the packages png and grid


Btw, this is just a cool and fast way to import different packages at once.

As an example for a background image plot I used the Sochi Olympic Medals plot by TRinker, which looks really good.

The tutorial shows you how to create a plot based on the current medals scores which looks like this:

sochi olympic games viz

Add an image

First of all we need to load the picture. Just place it in your working directory and load it with:

And add a raster:

And that´s nearly all!

Now we just have to add it with the annotation_custom() function to our plot.

The result will look like this:

sochi rstats image png gglot2

Julian Hillebrand

During my time at university and learning about the basics of economics I started heavily exploring the possibilities and changes caused by digital disruptions and the process of digital transformation, whereby I focused on the importance of data and data analytics and combination with marketing and management.
My personal focus of interest lies heavily on technology, digital marketing and data analytics. I made early acquaintance with programming and digital technology and never stop being interested in following the newest innovations.

I am an open, communicative and curious person. I enjoy writing, blogging and speaking about technology.

  • I wonder why some of the points (first pic) are not on the horizontal line. I don’t see any jitter commands anywhere?

    • Oh you are right. The first screenshot used a jitter graph I played around with. I will change the screenshot. Thanks for your comment.

  • Pingback: Add a background png image to ggplot2 | nishant@analyst()

  • Sandra

    cool tutorial! thanks for putting it on here!

  • Tae

    you’re so cool !! thanks for you, i could solve problems!

    I have one question. i want to add an image bottom the chart. please help me…!

    (i’m foreigner who speaks another language… i hope your help…. )