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:
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:
- use
saveMovie()to get a GIF animation (need ImageMagick) ani.start()andani.stop()can produce an HTML page with the animation in itsaveLatex()can embed an animation into a PDF document
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.
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:
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:
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=?.
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”.


Recent Comments