Playing with the ‘playwith’ package

[This article was first published on "R" you ready?, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 their blog: "R" you ready?.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)