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…
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:
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.
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(playwith) ## (for panel.usertext, etc)
## + 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
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)))
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”)
## draw custom annotations
panel.usertext(0.5206897, 0.06666667, “here is 9!”)
panel.arrows(0.4431034, 0.08034188, 0.2534483,0.1213675, length = 0.15, unit = “inches”)
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)
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.