Add a background png image to ggplot2

Profile photo of Julian Hillebrand
Follow me

Julian Hillebrand

I´m an International Business student from Germany, interested in Data Analytics and Machine Learning with a focus on Marketing Applications.
My favorite language is R.
Profile photo of Julian Hillebrand
Follow me

Latest posts by Julian Hillebrand (see all)

Share on FacebookTweet about this on TwitterShare on Google+Share on RedditShare on LinkedInEmail this to someone

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

Share on FacebookTweet about this on TwitterShare on Google+Share on RedditShare on LinkedInEmail this to someone
Profile photo of Julian Hillebrand

Julian Hillebrand

I´m an International Business student from Germany, interested in Data Analytics and Machine Learning with a focus on Marketing Applications. My favorite language is R.

You may also like...

5 Responses

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

  2. Sandra says:

    cool tutorial! thanks for putting it on here!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Skip to toolbar