Skip to content

Instantly share code, notes, and snippets.

@johnbaums
Last active December 31, 2015 23:40
Show Gist options
  • Save johnbaums/e799ebd3f9468a05e359 to your computer and use it in GitHub Desktop.
Save johnbaums/e799ebd3f9468a05e359 to your computer and use it in GitHub Desktop.
Add missing axes to a lattice multi-panel plot when as.table=TRUE
# See http://stackoverflow.com/a/34552387/489704 for motivation and an example
# Based on code provided by StackOverflow user @user20650
add_axes <- function() {
library(grid)
library(lattice)
l <- trellis.currentLayout()
pan <- which(l[nrow(l), ]==0)
if(length(pan) > 0) {
g <- grid.ls(print=FALSE)
ticks <- grid.get(g$name[grep("ticks.bottom.panel", g$name)][[1]])
labels <- grid.get(g$name[grep("ticklabels.bottom.panel", g$name)][[1]])
ax <- grobTree(ticks, labels)
invisible(sapply(pan, function(x) {
trellis.focus("panel", x, nrow(l)-1, clip.off=TRUE)
grid.draw(ax)
trellis.unfocus()
}))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment