# Time-stamp: <2010-04-29 17:30:15 chl> # # Simple wordle visualization in 3D. # Note: Require colorspace, rgl and tm packages. # library(colorspace) library(rgl) radius <- 100 generate.words <- function(min.freq=3) { loremipsum <- system.file("texts", "loremipsum.txt", package="tm") words <- scan(loremipsum, what="character") words <- gsub("[.,]","",words) words <- table(words) words <- words[words>min.freq] return(as.character(names(words))) } init.display <- function(col="white") { rgl.open() rgl.bg(color=col) } set.coord <- function(char) { n <- length(char) x <- y <- z <- numeric(n) for (i in 1:n) { alpha <- runif(1, 0, 2*pi) beta <- runif(1, 0, 2*pi) x[i] <- radius*(-cos(alpha))*sin(beta) y[i] <- radius*cos(beta) z[i] <- radius*sin(alpha)*sin(beta) } return(list(x=x,y=y,z=z)) } update.display <- function(char, animate=FALSE, save=FALSE, steps=5, ...) { coord <- set.coord(char) rgl.texts(coord$x, coord$y, coord$z, text=char, ...) if (animate) { M <- par3d("userMatrix") img.spin <- par3dinterp(userMatrix=list(M, rotate3d(M, pi/2, 1, 0, 0), rotate3d(M, pi/2, 0, 1, 0))) play3d(img.spin, duration=steps) if (save) movie3d(img.spin, duration=steps) } } words <- generate.words() my.cols <- sequential_hcl(length(words), power = 2.2) init.display("cornsilk") update.display(words, animate=TRUE, save=TRUE, col=my.cols) #rgl.close()