Creating Tag Cloud Using R and Flash / JavaScript (SWFObject)

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:

I was fortunate to get a very neat graph with no labels overlapping, but I don’t think this is a good solution, as it doesn’t take care of the initial locations of the words. My rough idea about deciding the initial locations is to sample on circles with radii proportional to the frequency, i.e. let and where . In this case, important words will be placed near the center of the plot.

2. Creating tag cloud in a Flash movie using R

The problem becomes quite easy with a Flash movie tagcloud.swf and a JavaScript program swfobject.js. The mechanism, briefly speaking, is that the tag information is passed to the Flash object by JavaScript, and the Flash object will read the variable tagcloud where the sizes, colors and hyperlinks of tags are stored. Finally the tags are visualized like rotating cloud.

It’s not difficult to pass the tag information to JavaScript in pure text. Below is the function which will create an HTML page by default with a tag cloud Flash movie inside it:

3. Example

Here is an example on visualizing my blog tags. You may need the following swf and js files first if you wish the loading would be faster (by default your browser needs to download these two files from roytanck.com first).

tagCloud(tagData)
# use tagCloud(tagData, SWFPath = "tagcloud.swf", JSPath = "swfobject.js")
# if you have downloaded these files to your work directory, i.e. getwd(),
# this will save you a few seconds loading the flash

The above code will generate an HTML page like this:

Your browser does not support Flash or Javascript!

You can adjust the parameters as you wish.

4. Other issues

There is still one more step to answer Tony’s original question, namely splitting the speech into single words and computing the frequency. This can be (roughly) done by strsplit(..., split = " ") and table().

Encoding problems may exist in the above code, but URLencode(tagXML) could be of help.

Only Latin characters are supported, but there’s possibility to modify the Flash source file to support other languages. See Roy Tanck’s post for more information.

Other R resources I know so far:

The R package R4X by Romain Fran?ois: you can generate an HTML page containing the tags with dynamic classes attached to the tags (install the package and read its vignette: install.packages('R4X', repos='http://r-forge.r-project.org'); vignette('r4xslides', package='R4X'))

The R package snippets by Simon Urbanek: there is a function cloud() to create word cloud; words are arranged from top to bottom and left to right