library(gridSVG) # General function to apply a function to each element of the # grid display list - should end up in 'grid' itself grid.DLapply <- function(fun, ...) { # Traverse DL and do something to each entry gridDL <- grid:::grid.Call.graphics("L_getDisplayList") gridDLindex <- grid:::grid.Call.graphics("L_getDLindex") for (i in 1:gridDLindex) { elt <- grid:::grid.Call.graphics("L_getDLelt", i) grid:::grid.Call.graphics("L_setDLindex", i) grid:::grid.Call.graphics("L_setDLelt", fun(elt, ...)) } grid:::grid.Call.graphics("L_setDLindex", gridDLindex) } # Add tooltip attributes to a grob on the DL garnishAllGrobs <- function(elt) { if (inherits(elt, "grob")) { garnishGrob(elt, onmousemove=paste("showTooltip(evt, '", gsub("\n", " ", elt$name), "')", sep=""), onmouseout="hideTooltip()") } else { elt } } addTooltips <- function(filename="Rplots.svg") { grid.DLapply(garnishAllGrobs) grid.script(filename="tooltip.js") gridToSVG(filename) } library(lattice) example(histogram) # Ctrl-c after first one addTooltips("tooltips.svg")