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

  • 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.
      Regards

  • 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…. )