-->

Tag Cloud



***

#------------------------------------------------------------------------------#
# generating tag cloud in R using Flash and SWFObject #
# tagData: a data.frame containing columns 'tag', 'link', 'count' and optional #
# columns 'color' and 'hicolor' #
# other parameters are self-explaining if you are familiar with #
# the WP plugin 'wp-cumulus' #
#------------------------------------------------------------------------------#
tagCloud = function(tagData, htmlOutput = "tagCloud.html",
SWFPath, JSPath, divId = "tagCloudId", width = 600, height = 400,
transparent = FALSE, tcolor = "333333", tcolor2 = "009900",
hicolor = "ff0000", distr = "true", tspeed = 100, version = 9,
bgcolor = "ffffff", useXML = FALSE, htmlTitle = "Tag Cloud",
noFlashJS, target = NULL, scriptOnly = FALSE) {
if (missing(SWFPath))
SWFPath = "http://www.roytanck.com/wp-content/plugins/wp-cumulus/tagcloud.swf"
if (missing(JSPath))
JSPath = "http://www.roytanck.com/wp-content/plugins/wp-cumulus/swfobject.js"
if (missing(noFlashJS))
noFlashJS = "This will be shown to users with no Flash or Javascript."
tagXML = sprintf("%s", paste(sprintf("%s",
tagData$link, tagData$count, if (is.null(target))
""
else sprintf(" target='%s'", target), if (is.null(tagData$color))
""
else ifelse(is.na(tagData$color), sprintf(" color='0x%s'",
tagData$color, ""), ""), if (is.null(tagData$hicolor))
""
else ifelse(is.na(tagData$hicolor), sprintf(" hicolor='0x%s'",
tagData$hicolor, ""), ""), tagData$tag), collapse = ""))
if (useXML)
cat(tagXML, file = file.path(dirname(htmlOutput), "tagCloud.xml"))
cat(ifelse(scriptOnly, "",
sprintf(" ?\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">


%s


",
htmlTitle)), sprintf("\t",
JSPath), sprintf("\t
%s
", divId,
noFlashJS), sprintf("\t\n",
SWFPath, width, height, version, bgcolor, ifelse(transparent,
"\t\tso.addParam(\"wmode\", \"transparent\");\n",
""), tcolor, tcolor2, hicolor, tspeed, distr, ifelse(useXML,
"\t\tso.addVariable(\"xmlpath\", \"tagcloud.xml\");",
sprintf("\t\tso.addVariable(\"tagcloud\", \"%s\");",
tagXML)), divId), ifelse(scriptOnly, "", "\n\n"),
file = ifelse(scriptOnly, stdout(), htmlOutput), sep = "\n")
}

The main argument is tagData which is a data.frame containing at least three columns (tag, link and count) and looks like:

> head(tagData)
tag link count
1 2D Kernel Density http://yihui.name/en/tag/2d-kernel-density/ 1
2 algorithm http://yihui.name/en/tag/algorithm/ 1
3 Animation http://yihui.name/en/tag/animation/ 11
4 AniWiki http://yihui.name/en/tag/aniwiki/ 2
5 Arcing http://yihui.name/en/tag/arcing/ 1
6 arrows() http://yihui.name/en/tag/arrows/ 1

Additional columns color and hicolor will be used if they exist (hexadecimal numbers specifying RGB), e.g.

> head(tagData)
tag link count color hicolor
1 2D Kernel Density http://yihui.name/en/tag/2d-kernel-density/ 1 2163bb f0763d
2 algorithm http://yihui.name/en/tag/algorithm/ 1 9f0f38 d825b1
3 Animation http://yihui.name/en/tag/animation/ 11 800130 5b8d6a
4 AniWiki http://yihui.name/en/tag/aniwiki/ 2 7ce1df 6607b0
5 Arcing http://yihui.name/en/tag/arcing/ 1 df4e4a f5cdf2
6 arrows() http://yihui.name/en/tag/arrows/ 1 31f5fb 19d50d

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).