if (interactive()) {
## time series plot (base graphics)
treering2 <- window(treering, start=1)
playwith(plot(treering2, xlim=c(0,300)),
labels = paste(time(treering2), "CE"))
## multi-panel lattice plot
playwith(xyplot(Income ~ log(Population / Area) | state.region,
data = data.frame(state.x77), subscripts=TRUE),
label.style = gpar(col="red", fontfamily="HersheySans",
cex=0.7))
## interactive control of a parameter with a slider
xx <- rnorm(50)
playwith(plot(density(xx, bw = bandwidth), panel.last = rug(xx)),
parameters = list(bandwidth = seq(0.05, 1, by=0.01)))
## the same with a spinbutton (use I() to force spinbutton).
## initial value is set as the first in the vector of values.
playwith(plot(density(xx, bw = bandwidth), panel.last = rug(xx)),
parameters = list(bandwidth = I(c(0.1, 5:100/100))) )
## more parameters
playwith(stripplot(yield ~ site, data = barley,
jitter=TRUE, type=c("p","a"),
aspect=aspect, groups=barley[[groups]],
scales=list(abbreviate=abbrev),
par.settings=list(plot.line=list(col=linecol))),
parameters = list(abbrev=FALSE, aspect=0.5,
groups=c("none", "year", "variety"),
linecol="red"))
## brushing a multivariate scatterplot
playwith(splom(environmental))
## simple spin and zoom for a 3D plot
playwith(wireframe(volcano, drape=TRUE))
## ggplot (NOTE: only qplot()-based calls will work)
## labels come from data frame automatically
library(ggplot2)
playwith(qplot(qsec, wt, data=mtcars) + stat_smooth())
## a minimalist grid plot
gridPlot <- function(x, y, xlim = NULL, ylim = NULL) {
if (is.null(xlim)) xlim <- extendrange(x)
if (is.null(ylim)) ylim <- extendrange(y)
grid.newpage()
pushViewport(plotViewport())
grid.rect()
pushViewport(viewport(xscale=xlim, yscale=ylim,
name="theData"))
grid.points(x, y)
grid.xaxis()
grid.yaxis()
upViewport(0)
}
playwith(gridPlot(1:10, 11:20), viewport="theData")
## acting like a dialog box (confirm close)
subTools <- list("identify", "clear", "zoom", "zoomout", "zoomfit")
confirmClose <- function(playState) {
if (gWidgets::gconfirm("Close window and report IDs?")) {
cat("Indices of identified data points:
")
print(rbind(playState$ids)$which)
all.ids <- do.call(rbind, playState$ids)$which
print(all.ids)
return(FALSE)
} else TRUE # don't close
}
xy <- list(x=rnorm(20), y=rnorm(20))
playwith(plot(xy), on.close=confirmClose, modal=TRUE,
win.size=c(360, 360), show.call=FALSE,
top=NULL, left=subTools)
## only local variables appearing in the plot call are stored
## (use eval.args=TRUE to store all data)
globalOne <- rnorm(1000)
localStuff <- function() {
localOne <- rnorm(1000)
playwith(plot(localOne, globalOne))
}
localStuff()
## list objects that have been copied and stored
sapply(playDevCur()$env, object.size)
## if global object is removed, redraws will fail
rm(globalOne)
## see help(playwith.API) for examples of new tools.
}
Run the code above in your browser using DataLab