Playing with the ‘playwith’ package


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 <- ""
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:


ylab="latitude",zlab="F1 (Hz)",
for (i in regs) {
d <- x[[i]]

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

About these ads

3 Responses to “Playing with the ‘playwith’ package”

  1. 1 Iago

    Do you know how could I show multiple plots with playwith? I want to store some histograms in a HTML file and then show them with Playwith…. but It only lets me display the last one. It stores them fine, but… as I said, when it comes to display them, I dont know how to do it. Any ideas? I read somethin about using layout and par.. but still doesnt show it right. Thanks

    for(i in 1:n) {
    png(paste(“hist”, i, “.png”, sep = “”))
    hist( as.matrix(numericos[i]),
    ylab = “Eje Y”, xlab = “Eje X “,

    main = colnames(numericos)[i]
    HTMLInsertGraph(paste(“hist”, i, “.png”, sep = “”),file=HTMLoutput,caption=captura)


    for(i in 1:n) {

    #autoplay(TRUE) Already tried with this,


  2. 2 nattomi

    Hi Iago, I’m not sure I understood your situation well, but if so, playwith(hist(as.matrix(numericos[i])),list(i=1:n)) should do what you want.

  3. 3 C S

    Just found your post on playwith – I had written up something too -dealing with more basic plots… Thanks for the writeup!


Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: