The
gridGraphics
Package

A little history ...

A little history ...

A little history ...

A little problem ...

library(lattice)
xyplot(mpg ~ disp, mtcars, pch=16)
abline(h=20)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...): 
  plot.new has not been called yet

A little history ...

A little history ...

A little problem ...

library(gridSVG)
plot(mpg ~ disp, mtcars, pch=16)
grid.export("plot.svg")
Error in grobToDev.default(gTree, dev): We shouldn't be here!

The gridGraphics package

User Guide

plot(mpg ~ disp, mtcars, pch=16)
library(gridGraphics)
grid.echo()

Advanced User Guide

pf <- function() plot(mpg ~ disp, mtcars, pch=16)
pushViewport(viewport(x=0, y=0, width=2/3, height=2/3,
                      just=c("left", "bottom")))
grid.echo(pf, newpage=FALSE)

Demonstration

plot(mpg ~ disp, mtcars) grid.echo()

Why ???

grid.ls()
graphics-plot-1-points-1
graphics-plot-1-bottom-axis-line-1
graphics-plot-1-bottom-axis-ticks-1
graphics-plot-1-bottom-axis-labels-1
graphics-plot-1-left-axis-line-1
graphics-plot-1-left-axis-ticks-1
graphics-plot-1-left-axis-labels-1
graphics-plot-1-box-1
graphics-plot-1-xlab-1
graphics-plot-1-ylab-1

Why ???

grid.edit("graphics-plot-1-bottom-axis-labels-1", rot=45)

Why ???

par(mfrow=c(3,6), oma=rep(1, 4), mar=c(1,1,1,1))
for(j in rownames(clusterprops)) {
    mosaicplot(CC ~ exposure, mosaicdata, ...)
}

Why ???

grid.echo()
grid.edit("text-1", grep=TRUE, global=TRUE,
          y=unit(1, "npc"), hjust=.2, vjust=0, rot=20)

Accessibility

Accessibility

Draw a map with maps, which uses graphics

states <- map("state", fill=TRUE, plot=FALSE)
statePolygons <- map2SpatialPolygons(states, IDs,
                     proj4string=CRS("+proj=longlat +datum=WGS84"))
plot(statePolygons)

Accessibility

Convert the map to grid with gridGraphics

grid.echo()

Accessibility (audio version)

Add audio tips and export to SVG with gridSVG

for (i in seq_along(stateGrobs)) {
    grid.garnish(stateGrobs[[i]],
                 onclick=paste0("speak(", stateNames[i], ")"))
}
grid.export("map-gridSVG-audio.svg")

Accessibility (tooltip version)

Add tooltips and export to SVG with gridSVG

for (i in seq_along(stateGrobs)) {
    grid.garnish(stateGrobs[[i]], title=stateNames[i])
}
grid.export("map-gridSVG-tooltips.svg")

Accessibility

Summary

Acknowledgements

The following R packages were used in the construction of these slides:

Thanks to Lize van der Merwe and Jonathon Godfrey for providing problems to my solution.

A little solution ...

plot(mpg ~ disp, mtcars, pch=16)
grid.echo()
grid.export("plot.svg")

A little solution ...

xyplot(mpg ~ disp, mtcars, pch=16,
       panel=function(...) { panel.xyplot(...); panel.abline(h=20) })

A little solution ...

print(xyplot(mpg ~ disp, mtcars, pch=16), prefix="mix-plot")
downViewport("mix-plot.panel.1.1.vp")
cvp <- current.viewport()
addLine <- function() {
    par(mar=rep(0, 4), xaxs="i", yaxs="i")
    plot.new()
    plot.window(xlim=cvp$xscale, ylim=cvp$yscale)
    abline(h=20)
}
grid.echo(addLine, newpage=FALSE)