GUI chart formatting with playwith

June 4, 2010
By

(This article was first published on R-Chart, and kindly contributed to R-bloggers)

There are a huge number of plots, annotations and parameters available in R for constructing charts and graphics. While looking through the R Programming wikibook I noticed a reference to a library called playwith. It provides a GUI that allows you to modify a graphs, including titles, lines, arrows, and options normally modified by plot or par parameters or other commands. It also allows you to see the R code required to construct the chart.

I decided to post on this because it was an approach to learning R commands that I had not seen mentioned elsewhere, and because installing and running playwith ended up being a bit more involved that I originally expected. However, I was working on a Windows OS which often can be a bit challenging when using software that has been developed by those from a Unix/Linux background...so perhaps this is to be expected.

After installing the playwith package, I was preparing to do my first test...

library(playwith)

A Windows error occurred referencing a missing DLL (libcairo-2.dll). A few minutes of googling led me to a number of links related to Cairo Graphics and GTK. Some comments at the download site indicated that you can have Cairo installed on Windows as a side-effect of installing GTK+. I was not sure of which files were necessary to install and which versions to use, so I proceeded to install the R RGTK2 package rather than sifting through the various GTK downloads that are available.

With RGTK2 installed successfully, I tried to load the library:

library(RGTK2)

Another pop-up window indicated that GTK needed installed - but this referenced a specific download from sourceforge. I accepted the prompt to download the GTK installation and restarted R. After this I was able to proceed with running playwith - at least some of the features (e.g. other packages are required to take advantage of all of the functionality available options under the Style menu). Anyway, playwith is simple enough to use - simply put your plot call inside of a call to playwith.

playwith(barplot(c(9,3,1,2,3,6,7)))





After a few minutes of navigating around the GUI I was able to add a number of labels and annotations (far quicker than I would have if I looked up commands and plotted positions manually).





The File --> Save Code menu option resulted in an R source file being saved to disk that was more or less usable. However, it did not include all of the libraries that need to be used (a possibility suggested by the comments). My chart ended up requiring the gridBase package. Once I added this to the code, it executed properly and reproduced the chart I had designed in the playwith GUI.

The modified source code to produce the chart is as follows:

library(grid)
library(lattice)
library(playwith) ## (for panel.usertext, etc)
library(gridBase)

## + might need others, often library(latticeExtra).
## Assuming that the data are attached and any
## customised style settings are in place; save with
## myStyle <- trellis.par.get(); then restore with

## trellis.par.set(myStyle)
barplot(c(9, 3, 1, 2, 3, 6, 7), sub = "A Sub", xlab = "An XLab", ylab = "A Ylab")

## set up viewports
pushViewport(viewport(name = "pageAnnotationVp", yscale = c(1, 0)))
upViewport(0)
local({
vps <- baseViewports()
vps$plot$name <- "plot"
vps$plot$clip <- TRUE
vps$plot.clip.off <- viewport(xscale = par("usr")[1:2],
yscale = par("usr")[3:4], clip = "off",
name = "plot.clip.off")
pushViewport(do.call("vpStack", vps))
})

## draw custom annotations
seekViewport("pageAnnotationVp")
panel.usertext(0.5206897, 0.06666667, "here is 9!")
panel.arrows(0.4431034, 0.08034188, 0.2534483,0.1213675, length = 0.15, unit = "inches")
seekViewport("plot")
panel.rect(2.259542, 4.14073, 5.566833, 7.608541)
panel.rect(2.410733, 5.029356, 4.886476, 7.456824)
panel.rect(4.999869, 6.914979, 5.377845, 7.413476)
panel.rect(4.999869, 6.373133, 5.340047, 6.784936)
panel.segments(3.752548, 7.630215, 5.944809, 8.670558)
panel.segments(3.71475, 7.651888, 3.091089, 8.757253)
panel.segments(2.921, 8.605536, 3.147786, 8.887296)
panel.segments(5.793618, 8.822275, 6.039303, 8.583863)
panel.usertext(2.939899, 6.286438, " Where's _Why?",
col = "green3", srt = -30)
upViewport(0)

All in all, a worthwhile package to investigate. You might find it useful if you are new to R and want to get an idea of some of the basic commands that can be used to modify and annotate a chart. It also might be helpful if you have an existing plot that you want to annotate visually. Just keep in mind that the dependencies for running the application and for running resulting code may not all be available, so plan for some time spent working these out.

To leave a comment for the author, please follow the link and comment on his blog: R-Chart.

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

Tags: ,

3 Responses to GUI chart formatting with playwith

  1. webbedfeet (Abhijit) on June 5, 2010 at 9:08 am

    Twitter Comment


    ♺ @Rbloggers: GUI chart formatting with playwith: There are a huge number of plots, annotations and… [link to post] #rstats

    Posted using Chat Catcher

  2. Rbloggers (R bloggers website) on June 5, 2010 at 10:01 am

    Twitter Comment


    GUI chart formatting with playwith: There are a huge number of plots, annotations and… [link to post] #rstats

    Posted using Chat Catcher

  3. ResearchProcess (RoR Group) on June 5, 2010 at 2:59 pm

    Twitter Comment


    GUI chart formatting with playwith [link to post] Very useful for editing graphics

    Posted using Chat Catcher