Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/1873a7b59a77f51c4a78 to your computer and use it in GitHub Desktop.
Save timelyportfolio/1873a7b59a77f51c4a78 to your computer and use it in GitHub Desktop.
modify lattice R splom to draw axis ticks and tick labels on outside of entire plot

In response to a tweet, try to draw a R lattice splom or pairs plot with the ticks and tick labels on the outside of the superpanel or the entire graph. I could not find any examples, so I resorted to the source code. I still could not find an acceptable answer, but this gets as close as possible.

require(lattice)
require(grid)
#this setting controls whether it will show up or not
#default is panel = "on", strip = "on"
trellis.par.set("clip",list(panel="off",strip="off"))
#almost exact copy of diag.panel.splom from lattice package
#https://r-forge.r-project.org/scm/viewvc.php/pkg/R/splom.R?view=markup&root=lattice
#but uses outside=T for axis
diag.panel.splom2 <-
function(x = NULL,
varname = NULL, limits, at = NULL, labels = NULL,
draw = TRUE, tick.number = 5,
varname.col = add.text$col,
varname.cex = add.text$cex,
varname.lineheight = add.text$lineheight,
varname.font = add.text$font,
varname.fontfamily = add.text$fontfamily,
varname.fontface = add.text$fontface,
axis.text.col = axis.text$col,
axis.text.alpha = axis.text$alpha,
axis.text.cex = axis.text$cex,
axis.text.font = axis.text$font,
axis.text.fontfamily = axis.text$fontfamily,
axis.text.fontface = axis.text$fontface,
axis.text.lineheight = axis.text$lineheight,
axis.line.col = axis.line$col,
axis.line.alpha = axis.line$alpha,
axis.line.lty = axis.line$lty,
axis.line.lwd = axis.line$lwd,
axis.line.tck = 1,
...)
{
add.text <- trellis.par.get("add.text")
axis.line <- trellis.par.get("axis.line")
axis.text <- trellis.par.get("axis.text")
if (!is.null(varname))
grid.text(varname,
name = trellis.grobname("diag.text", type="panel"),
gp =
gpar(col = varname.col,
cex = varname.cex,
lineheight = varname.lineheight,
fontface = lattice:::chooseFace(varname.fontface, varname.font),
fontfamily = varname.fontfamily))
if (draw) ## plot axes
{
rot <- if (is.numeric(limits)) 0 else c(90, 0)
axis.details <-
lattice:::formattedTicksAndLabels.default(limits,
at = if (is.null(at)) TRUE else at,
labels = if (is.null(labels)) TRUE else labels,
logsc = FALSE,
..., n = tick.number)
for (side in c("left", "bottom")) #,top", "right", "bottom"))
panel.axis(side = side,
at = axis.details$at,
labels = axis.details$labels,
check.overlap = axis.details$check.overlap,
ticks = TRUE,
half = F,
outside = T,
tck = axis.line.tck,
rot = rot,
text.col = axis.text.col,
text.alpha = axis.text.alpha,
text.cex = axis.text.cex,
text.font = axis.text.font,
text.fontfamily = axis.text.fontfamily,
text.fontface = axis.text.fontface,
text.lineheight = axis.text.lineheight,
line.col = axis.line.col,
line.alpha = axis.line.alpha,
line.lty = axis.line.lty,
line.lwd = axis.line.lwd)
}
}
splom(
~iris[1:4]
, groups = Species
, data = iris
, panel = function(x,y,i,j,...){
#if in column 1 or row 1 make an axis
if (i == 1 ) {#|| (i == 2 && j == 1) ) {
panel.axis(
side = "bottom"
,at = pretty(x)
,check.overlap=T
,half=F
,outside=T
)
}
if (j == 1) {
panel.axis(
side = "left"
,at=pretty(y)
,check.overlap=T
,half=F
,outside=T
)
}
panel.xyplot(x,y,...)
#print(as.list(match.call()))
}
, diag.panel = function(varname,draw,...){
#do draw = F since already drawing axis on outer edges
#of superpanel
#more manual than I would like but we only want x
#in first diag panel so
#in case of iris set varname == "Sepal.Length"
if(varname=="Sepal.Length") draw = T else draw = F
diag.panel.splom2(varname,draw =draw,...)
#print(as.list(match.call()))
#print(varname)
}
,xlab=" "
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment