Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I overlay an image on to a ggplot?

Tags:

r

ggplot2

png

I'd like to read an image from the web. e.g.

http://api.altmetric.com/donut/502878_64x64.png

and insert it into the top right of a ggplot

df <- data.frame(x=1:10, y=sample(1:100,10))
# a fake plot to try it on.
ggplot(df, aes(x,y)) + geom_point(size = 2)

How would I do this?

like image 947
Maiasaura Avatar asked Sep 28 '12 04:09

Maiasaura


People also ask

How do I insert an image into a plot in R?

To add a picture to a plot in base R, we first need to read the picture in the appropriate format and then rasterImage function can be used. The most commonly used format for picture in R is PNG. A picture in PNG format can be added to a plot by supplying the values in the plot where we want to add the picture.

What does Ggplot () do?

ggplot() initializes a ggplot object. It can be used to declare the input data frame for a graphic and to specify the set of plot aesthetics intended to be common throughout all subsequent layers unless specifically overridden.

Does Ggplot only work with data frames?

ggplot only works with data frames, so we need to convert this matrix into data frame form, with one measurement in each row. We can convert to this “long” form with the melt function in the library reshape2 . Notice how ggplot is able to use either numerical or categorical (factor) data as x and y coordinates.


3 Answers

You are looking for annotation_raster and readPNG

mypngfile <- download.file('http://api.altmetric.com/donut/502878_64x64.png', destfile = 'mypng.png', mode = 'wb')
library(png)
mypng <- readPNG('mypng.png')


p <- qplot(mpg, wt, data = mtcars) + theme_bw()
p + annotation_raster(mypng, ymin = 4.5,ymax= 5,xmin = 30,xmax = 35) + 
    geom_point()

enter image description here

These new features (and more examples) are described here

like image 66
mnel Avatar answered Oct 01 '22 17:10

mnel


The correct solution was this:

# This was one of my issues, reading a png from the web
my_image <-  readPNG(getURLContent('http://path.to/image.png'))
p1 + annotation_raster(my_image, ymin = 4,ymax= 5,xmin = 30,xmax = 40)
like image 29
Maiasaura Avatar answered Oct 01 '22 19:10

Maiasaura


Just adding an update from the terrific package Magick, which will even allow a GIF to be overlaid on ggplot images:

library(ggplot2)
library(magick)
library(here) # For making the script run without a wd
library(magrittr) # For piping the logo

# Make a simple plot and save it
ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point() + 
  ggtitle("Cars") +
  ggsave(filename = paste0(here("/"), last_plot()$labels$title, ".png"),
         width = 5, height = 4, dpi = 300)

# Call back the plot
# Now call back the plot
background <- image_read(paste0(here("/"), "Cars.png"))
# And bring in a logo
logo_raw <- image_read("https://i.imgur.com/e1IneGq.jpg") 

frames <- lapply(logo_raw, function(frame) {
  image_composite(background, frame, offset = "+70+800")
})

animation <- image_animate(image_join(frames))

image_write(animation, "~/Cars_Travolta.gif")
like image 36
D.Hadley Avatar answered Oct 01 '22 17:10

D.Hadley