2

I am trying to import a gif and turn it into a grob to use it with the function annotation_custom in a ggplot, so far I imported it and have been able to turn it into an image.

So far this is what I have:

library(caTools)
require(ggplot2)
require(grid)
require(gridExtra)
y <- read.gif("http://blog.helpingadvisors.com/wp-content/uploads/2011/01/basketballcourt.gif")

and I get a list, so far I have been able to plot it:

image(y$image, col=y$col, main=y$comment, asp=1)

But I have tried to transform it to a grob:

a <- rasterGrob(image(y$image, col=y$col, main=y$comment, asp=1))

or try

raster(image(y$image, col=y$col, main=y$comment, asp=1))

and then save it as a grob, but since image returns class NULL it does not work.

I've also tried to use the matrix from y, but it gets really strange colours:

plot(raster(y$image))

and I really need the original colours

Derek Corcoran
  • 3,930
  • 2
  • 25
  • 54

1 Answers1

2
mat = y$col[y$image+1]
dim(mat) = dim(y$image)
qplot(1,1) + annotation_custom(rasterGrob(mat))

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294