Playing with the ‘playwith’ package

March 23, 2010
By

(This article was first published on "R" you ready?, and kindly contributed to R-bloggers)

Abilities of R for creating graphics is great, but one thing I always missed is the possibility of creating interactive plots and being able to look at graphs while changing one ore more parameters. I know that there is rggobi, but so far I always ran into problems with flexibility each time I wanted to use it. So I kept on searching until I found playwith which is “an R package, providing a GTK+ graphical user interface for editing and interacting with R plots” as its homepage says. The homepage includes a lot of screenshots with code snippets so this post doesn’t intend to give an extensive review about the possibilities of the playwith package to the reader. All I want to do now is present a small application of it.

I had some geospatial data I wanted to visualize. The data was a result of a computer simulation and consisted of a set of geographical coordinates and corresponding frequency values expressed in Hz. The values associated to the coordinates (x,y)  tells us the first eigenfrequency of the Earth-ionosphere cavity that would be measured at Nagycenk Observatory in the case of an assumed lightning source at (x,y) with certain properties.

At first, as always, we need to load some packages.

library(R.basic) # for creating perspective plot
library(playwith) # for creating interactive plot
library(fields) # for plotting a map of the world

Then, we read in our data (which is available online, so the example must be reproducible):


regs <- list(Africa="Africa",Americas="Americas",Asia="Asia")
cols <- c(Africa="red",Americas="green",Asia="blue")
url <- "http://storage.ggki.hu/~nattomi/ryouready/20100303"
x <- lapply(regs, function(x) {read.table(file.path(url,x),header=TRUE)})

Before plotting, I determine the range of the plottted values.

data(world.dat) # data used for plotting a world map
zAxs <- unlist(lapply(x,function(x) x$fnERT1))
r <- range(zAxs)

And finally, the interactive plot itself:

playwith({plot3d(world.dat$x,world.dat$y,lowZ,pch=".",
zlim=c(lowZ,upZ),xlab="longitude",

ylab="latitude",zlab="F1 (Hz)",
theta=theta,phi=phi,ticktype="detailed")
for (i in regs) {
d <- x[[i]]
points3d(d$Dc,d$Hc,d$fnERT1,col=cols[[i]],pch=".")
}},
parameters=list(
theta=seq(0,360,by=5),
phi=seq(0,90,by=5),
lowZ=seq(r[1],r[2],0.5),
upZ=seq(r[2],r[1],-0.5)))

You should see a window popping up with 4 sliders allowing you to set different paramters of the plot, for example vertical and horizontal rotation. You can already see from the example that the syntax of the playwith command is very simple, you specify a set of commands necessary for creating the plot (with possible parameters included such as theta in this example) between curly braces then a list specifying values to be looped through for the parameters. What more could I say? If you are a visual type (or your boss is one) then play with playwith!

Remark 1: Installing the package R.basic goes in a little bit unusual way, see http://www.braju.com/R/ for details.

Remark 2: My world map is just a plot of a cloud of points on the plane. It would be nice if the points would be connected accordingly. This can be achieved by using the world() command in the fields package although I wasn’t able to integrate this into the 3d display. Any suggestions are very welcome. The world.dat dataset has an another drawback: it doesn’t include Corsica and Sardinia so this world map is not of too much use for the locals.


To leave a comment for the author, please follow the link and comment on his blog: "R" you ready?.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.