Overlay data onto background image

JPEG

For jpeg images, you can use read.jpeg() from the rimage package.

eg :

anImage <- read.jpeg("anImage.jpeg")
plot(anImage)
points(my.x,my.y,col="red")
...

By setting par(new=T) before the next plot command, you can construct complete plots over a background picture. (see ?par and further down)

PNG

PNG images you can upload using readPNG from the png package. With readPNG, you need the rasterImage command to plot (see also the help files). On Windows, one has to get rid of the alpha channel, as Windows cannot cope with per-pixel alphas up to now. Simon Urbanek was so kind as to point out this solution :

img <- readPNG(system.file("img", "Rlogo.png", package="png"))
r = as.raster(img[,,1:3])
r[img[,,4] == 0] = "white"

plot(1:2,type="n")
rasterImage(r,1,1,2,2)

GIF

For gif files, you can use read.gif from caTools. Problem is that this is rotating the matrix, so you have to adjust it :

Gif <- read.gif("http://www.openbsd.org/art/puffy/ppuf600X544.gif")

n <- dim(Gif$image)
image(t(Gif$image)[n[2]:1,n[1]:1],col=Gif$col,axes=F)

To plot over this image, you have to set the par correctly, eg :

image(t(Gif$image)[n[2]:1,n[1]:1],col=Gif$col,axes=F)
op <- par(new=T)
plot(1:100,new=T)
par(op)

Leave a Comment