Skip to content

Instantly share code, notes, and snippets.

@MrFlick
Last active August 29, 2015 14:00
Show Gist options
  • Save MrFlick/45fece32487993b3929e to your computer and use it in GitHub Desktop.
Save MrFlick/45fece32487993b3929e to your computer and use it in GitHub Desktop.
getExpressionStrip.R: allows for the easy inclusion of math (expressions) in Lattice panel strips
getExpressionStrip <- function(...) {
dots<-list(...)
if (length(dots)==0) return(strip.default)
if (class(dots[[1]])=="list") {
rename <- dots[[1]]
stripparam <- dots[-1]
} else {
rename <- dots
stripparam <- list()
}
function(...,factor.levels, var.name) {
rn<-function(x) if (x %in% names(rename)) rename[[x]] else x;
factor.levels<-do.call(c, lapply(factor.levels, rn))
var.name<-do.call(c, lapply(var.name, rn))
 
xx<-c(stripparam, list(factor.levels=factor.levels, var.name=var.name), list(...))
return(do.call(strip.default, xx))
}
}
require(lattice)
#sample data
dd<-data.frame(rsq=rep(c("alpha","betaplus1","gamma"), 4), delta=rep(c("High","Low"), each=6), x=rep(1:2, 6), y=runif(12))
#basic syntax
xyplot(y~x|rsq+delta, dd,
strip=getExpressionStrip(alpha=expression(alpha), betaplus1=expression(beta + 1), gamma=expression(gamma))
)
#extended syntax
xyplot(y~x|rsq+delta, dd,
strip=getExpressionStrip(
list(delta=expression(Delta), rsq=expression(r^2),
alpha=expression(alpha), betaplus1=expression(beta+1), gamma=expression(gamma)
),
strip.names=T
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment