Dec 312009

I have to admit that the previous post on Christmas is actually not much fun. Today I received another pResent from Yixuan which is more interesting:

Dec 242009

Life should be fun. I saw a post in R-help list saying Merry Christmas to other useRs, and I followed up by some R code which can produce a naive animation like this:

Here is the code to generate the above Flash animation with shining Christmas:

library(animation)
saveSWF({
    n = length(speed <- runif(angle <- runif(x <- strsplit("MERRY CHRISTMAS",
        "")[[1]], 0, 360), 0, 15))
    for (j in 1:300) {
        angle = angle + speed
        plot.new()
        plot.window(c(1, n), c(0, 1))
        for (i in 1:n) text(i, 0.5, x[i], srt = angle[i], cex = runif(1,
            1, 4), col = sample(colors(), 1))
        text(n, 0, "Yihui @ 2009-12-24 (http://yihui.name)",
            adj = c(1, 0), col = "white", cex = 0.8)
    }
}, interval = 0.04, dev = "pdf", outdir = getwd(), para = list(mar = rep(0,
    4), bg = "black"), width = 8, height = 1)
## in animation package (>=1.1-0), see demo('Xmas')

There are other animation formats in the R package animation:

  1. use saveMovie() to get a GIF animation (need ImageMagick)
  2. ani.start() and ani.stop() can produce an HTML page with the animation in it
  3. saveLatex() can embed an animation into a PDF document
Oct 192009

I love R because there are always exciting new packages which can be far beyond your imagination. Here I’d like to introduce a couple of packages that look really awesome:

1. swfDevice: R graphics device for SWF output (by Cameron Bracken)

This package is still at a pre-alpha stage but you can see a sketch now in R-Forge: https://r-forge.r-project.org/projects/swfdevice/

Its author, Cameron, certainly knows well that I will be excited to see it, because I’ve been waiting for a long long time for the REAL Flash animation output in R. What I’ve done in my animation package is simply using SWF Tools to combine several “static” pictures (PNG or PDF, …) into a naive Flash animation — by “naive” I mean there is no interaction or real dynamic stuff in the Flash animation. Hopefully Cameron will provide a useful tool to create genuine Flash animations directly from R (with the help of the library libming).

By the way, I have to mention that the tikzDevice package by Cameron and another author is also fantastic for generating high-quality graphics LaTeX.

Jun 122009
Linlin Yan posted a cool (hot?) simulation of burning fire with R in the COS forum yesterday, which was indeed a warm welcome. I’m not sure whether our forum members will be scared by the “fire” under the title “Welcome to COS Forum”. :grin: The fire was mainly created by the function image() with carefully designed rows and columns in heated colors heat.colors(). Here is one of the pictures generated from his code:

Simulation of Burning Fire in R

Simulation of Burning Fire in R

Jun 102009
Tag cloud is a bunch of words drawn in a graph with their sizes proportional to their frequency; it’s widely used in blogs to visualize tags. We can observe important words quickly from a tag cloud, as they often appear in large fontsize. Tony N. Brown asked how to “graphically represent frequency of words in a speech” the other day in R-help list, which is actually a problem about the tag cloud:

I recently saw a graph on television that displayed selected words/phrases in a speech scaled in size according to their frequency. So words/phrases that were often used appeared large and words that were rarely used appeared small. [...]

Marc Schwartz mentioned that Gorjanc Gregor has done some work years ago using R (in grid graphics). The obstacle of creating tag cloud in R, as Gorjanc wrote, lies in deciding the placement of words, and it would be much easier for other applications such as browsers to arrange the texts. That’s true — there have already been a lot of mature programs to deal with tag cloud. One of them is the wp-cumulus plugin for WordPress, which makes use of a Flash object to generate the tag cloud, and it has fantastic 3D rotation effect of the cloud.

1. Arranging text labels with pointLabel()

Before introducing how to port the plugin into R, I’d like to introduce an R function pointLabel() in maptools package and it can partially solve the problem of arranging text labels in a plot (using simulated annealing or genetic algorithm). Here is a simulated example:

Simulated Tag Cloud with R function pointLabel() in maptools

Simulated Tag Cloud with R function pointLabel() in maptools

Jun 202008

To convert PDF to SWF, we may use the utility pdf2swf in SWF Tools. However, the feature of specifying the frame rate has not been documented yet. The other day Matthias Kramm told me this option just hided behind -s.

Usage: pdf2swf [-options] file.pdf -o file.swf

-h , --help               Print short help message and exit
-V , --version            Print version info and exit
....
-p , --pages range        Convert only pages in range with range e.g. 1-20 or 1,4,6,9-11 or
....
-s , --set param=value    Set a SWF encoder specific parameter.
                         ?See pdf2swf -s help for more information.
....

To specify the frame rate, we just need to use pdf2swf -s framerate=?.

Jun 132008

The SWF Tools has provided several SWF utilities for the manipulation and creation of Flash files. Today I just wrote a wrapper saveSWF() in the package “animation” to convert image frames to Flash animations. Here is an example for the kNN algorithm:

I’d like to thank Hadley for telling me this tool set. The function saveSWF() is to appear in animation 1.0-1.

Till now, there are four kinds of animations in the animation package: (1) animations inside R windows graphics devices; (2) animations in HTML pages (driven by JavaScript); (3) GIF or AVI animations with the help of “ImageMagic”; (4) Flash animations with the help of “SWF Tools”.

WWW.YIHUI.NAME XIE@YIHUI.NAME © 2007 - 2010 by Yihui Xie