10 Oct 2011

Plot Animation with Imported Images

...I really dig the animation package! ..so here's the outcome of my firsts encounters with saveHTML() - I produced an animation with pre-existing images by utilizing the functions readJPEG() and rasterImage() from the R-packages jpeg and ReadImages. Credit goes out to xingmowang (nzprimarysectortrade-blog) from whom I picked up the concept of putting images to the plot region of a graph produced with the animation-functions.













# load packages:
require(ReadImages)
require(animation)
require(jpeg)

# set directory (mind to change path!):
setwd("C:\\Users\\Kay\\Documents\\R.Examples\\Animation")

# the below created pictures could actually be produced within the
# saveHTML() call - but as i wanted to illustrate how pre-existent
# images would be used via readJPG() import and plotting by rasterImage()
# a had to make up some example jpegs (# images produced will
# use about 4 mb memory):

# xy coordinates of a sin-curve:
x <- seq(from = -4, to = 4, by = 0.1)
xy <- data.frame(x = x, y = sin(x))
plot(xy$x, xy$y, type = "l")
nrow(xy)

# make images:
for (i in 1:nrow(xy)){
jpeg(paste(getwd(),"\\", i, "_pic.jpg", sep = ""),
     width = 10, height = 10, units = "in", res = 100)
plot(xy$x[1:i], xy$y[1:i], ylim = c(-1.1, 1.1), xlim = c(-4.1, 4.1),
     col = 3, lwd = 4, type = "l")
dev.off()}

# now look up what's in dir:
dir()

# get the created jpg's names:
files <- dir()
print(pics <- files[grep(".jpg", files, ignore.case = T)])

# you see that the sorting of names is not sequential as we want..
# so i'll extract pic-numbers and sort pics accordingly:
pics <- pics[order(as.integer(sub("_.*", "", pics)))]


# produce HTML:
# margins and plot axes will be plotted for illustration of
# the img-positioning within the plot region:
# fetch the image and align at x0, y0, x1, y1 plot coordinates:

saveHTML({
         for(i in pics){
                        tmp <- readJPEG(i)
                        par(bg = "thistle", mar = c(2, 2, 2, 2))
                        plot(c(0, 10), c(0,10), type="n",
                             bty="n", xlab="", ylab="",
                             yaxs ="i", xaxs = "i")
                        rasterImage(tmp, 0, 0, 10, 10)
                       }
         },
         img.name = "pic",
         interval = 0.2,
         htmlfile = "test.html",
         outdir = getwd(),
         title = "Demo",
         autobrowse = FALSE,
         description = c("This is an animtated curve\n",
                         "...you can describe it here"))

# for producing flash animation or mp4-video see ?saveSWF() and ?saveVideo()
# for this you'll need different convertion programs, 
# as given in the help-files..
To cite package ‘animation’ in publications use:

  Yihui Xie (2011). animation: A Gallery of Animations in Statistics
  and Utilities to Create Animations. R package version 2.0-5.
  http://CRAN.R-project.org/package=animation

To cite package ‘jpeg’ in publications use:

  Simon Urbanek (). jpeg: Read and write
  JPEG images. R package version 0.1-2. http://www.rforge.net/jpeg/

To cite package ‘ReadImages’ in publications use:

  Markus Loecher (2009). ReadImages: Image Reading Module for R. R
  package version 0.1.3.1. http://CRAN.R-project.org/package=ReadImages

2 comments :

  1. Hi Kay,

    Thanks for quoting me here. YiHui Xie is the man. Haha

    A hint would be: instead of reading .jpeg files, you make the graphs in R directly,
    i.e.,
    x <- sin(seq(0,2*pi,len=50))
    saveHTML({plot(x[1:i],type="l",
    col="green")
    },
    ...
    )

    I used read.jpeg simply because I just had these pictures and they cannot be produced using R.

    Really like your blog!

    ReplyDelete
  2. Hi,

    Actually this was my point: to show how to use pre-existing images. And, as commented somewhere in the post, I just made up some fake pics for this purpose (as to have a "self-standing" script example..).

    Thanks and best wishes,
    KC

    ReplyDelete