 
       
      library(lattice) xyplot(mpg ~ disp, mtcars)
library(grid) grid.ls()
plot_01.background plot_01.xlab plot_01.ylab plot_01.ticks.top.panel.1.1 plot_01.ticks.left.panel.1.1 plot_01.ticklabels.left.panel.1.1 plot_01.ticks.bottom.panel.1.1 plot_01.ticklabels.bottom.panel.1.1 plot_01.ticks.right.panel.1.1 plot_01.xyplot.points.panel.1.1 plot_01.border.panel.1.1
Some other functions that help with exploring grobs:
| grid.grep(path) | Search for a grob that matches 'path'. | 
| showGrob(gPath) | Highlight grob that matches 'gPath'. | 
| grobBrowser() | SVG version with grob names as tooltips (from the 'gridDebug' package). | 
xyplot(mpg ~ disp, mtcars)
grid.ls(viewports=TRUE, grobs=FALSE)
  ROOT
    plot_01.toplevel.vp
      plot_01.xlab.vp
      plot_01.ylab.vp
      plot_01.figure.vp
      plot_01.panel.1.1.vp
      plot_01.strip.1.1.off.vp
      plot_01.strip.left.1.1.off.vp
      plot_01.panel.1.1.off.vp
grid.ls(viewports=TRUE, fullNames=TRUE)
  viewport[ROOT]
    rect[plot_01.background]
    viewport[plot_01.toplevel.vp]
      viewport[plot_01.xlab.vp]
        text[plot_01.xlab]
        upViewport[1]
      viewport[plot_01.ylab.vp]
        text[plot_01.ylab]
        upViewport[1]
      viewport[plot_01.figure.vp]
Some other functions that help with exploring viewports:
| showViewport(vp) | Highlight viewport that matches 'vp'. | 
| current.viewport() | Returns the current viewport. | 
Ideally, a package will document the naming scheme that it uses for grobs and viewports. Ideally, a package will have a naming scheme!
The purpose of this exercise is to make use of the grid.ls() function.
The following code creates a 'lattice' scatterplot:
library(lattice) xyplot(mpg ~ disp, mtcars, main="Fast Cars")
library(lattice) barchart(Party ~ Amount_Donated, sortedTotals)
library(grid) grid.ls()
plot_01.background plot_01.xlab plot_01.ticks.top.panel.1.1 plot_01.ticklabels.left.panel.1.1 plot_01.ticks.bottom.panel.1.1 plot_01.ticklabels.bottom.panel.1.1 plot_01.abline.v.panel.1.1 plot_01.barchart.abline.v.panel.1.1 plot_01.barchart.rect.panel.1.1 plot_01.border.panel.1.1
library(grid) grid.remove("plot_01.border.panel.1.1")
Functions that can be used to access grobs:
| grid.remove() | Remove a grob. | 
| grid.edit() | Modify a grob component. | 
| grid.get() | Get a copy of a grob component. | 
| grid.set() | Replace a grob component. | 
library(grid) t <- grid.get("plot_01.ticklabels.bottom.panel.1.1") names(t)
[1] "label" "x" "y" "just" [5] "hjust" "vjust" "rot" "check.overlap" [9] "name" "gp" "vp"
t$just
[1] "centre" "top"
library(grid) grid.edit("plot_01.ticklabels.bottom.panel.1.1", just=c("left", "top"))
library(grid) gpar(col="blue", lwd=3, lty="dashed")
$col [1] "blue" $lwd [1] 3 $lty [1] "dashed"
Common gpar() settings:
| col | (border) colour. | 
| fill | fill colour. | 
| lty | line type. | 
| lwd | line width. | 
| cex | text size multiplier. | 
library(grid) grid.edit("plot_01.ticklabels.bottom.panel.1.1", gp=gpar(col="grey"))
The purpose of this exercise is to make use of the grid.edit() and grid.remove() functions.
The following code creates a 'lattice' scatterplot:
library(lattice) xyplot(mpg ~ disp, mtcars, main="Fast Cars")
This is the result you are looking for (before you remove the title):
          
        
 
        Credit: John G. Bullock, Yale University.
 
        Credit: John G. Bullock, Yale University.
xyplot(mpg ~ disp, mtcars)
grid.ls(viewports=TRUE, fullNames=TRUE)
  viewport[ROOT]
    rect[plot_01.background]
    viewport[plot_01.toplevel.vp]
      viewport[plot_01.xlab.vp]
        text[plot_01.xlab]
        upViewport[1]
      viewport[plot_01.ylab.vp]
        text[plot_01.ylab]
        upViewport[1]
      viewport[plot_01.figure.vp]
downViewport("plot_01.toplevel.vp") grid.rect(gp=gpar(col=NA, fill=rgb(0,1,0,.5)))
downViewport("plot_01.panel.1.1.vp") grid.rect(gp=gpar(col=NA, fill=rgb(1,0,0,.5)))
upViewport() grid.rect(gp=gpar(col=NA, fill=rgb(0,0,1,.5)))
The purpose of this exercise is to make use of the downViewport() function.
The following code creates a 'lattice' scatterplot:
library(lattice) xyplot(mpg ~ disp, mtcars, main="Fast Cars")
This is the result you are looking for:
          
        
 
        Credit: Pascal A. Niklaus, University of Zurich.
 
        Credit: Pascal A. Niklaus, University of Zurich.
 
        Credit: Brad Boehmke.
xyplot(mpg ~ disp, mtcars) downViewport("plot_01.panel.1.1.vp")
grid.text("native", x=unit(300, "native"), y=unit(30, "native"), just=c("left", "bottom"))
grid.text("absolute", x=unit(1, "in"), y=unit(1, "cm"), just=c("left", "bottom"))
grid.text("normalised", x=unit(.75, "npc"), y=unit(.5, "npc"), just=c("left", "bottom"))
grid.text("normalised - absolute", x=unit(1, "npc") - unit(1, "cm"), y=unit(1, "npc") - unit(1, "cm"), just=c("right", "top"))
Some basic shapes that 'grid' can draw:
| grid.rect(x,y,w,h) | rectangles. | 
| grid.circle(x,y,r) | circles. | 
| grid.lines(x,y) | straight lines through (x,y). | 
| grid.segments(x0,y0,x1,y1) | straight lines from (x0,y0) to (x1,y1). | 
| grid.text(label,x,y) | text. | 
grid.rect(x=unit(.2, "npc"), y=unit(100, "native"), width=unit(1, "in"), height=unit(1, "lines"))
grid.circle(x=1:5/6, y=.5, r=1:5/25)
The purpose of this exercise is to make use of the unit() function as well as 'grid' functions that draw basic shapes.
The following code creates a 'lattice' scatterplot:
library(lattice) xyplot(mpg ~ disp, mtcars, main="Fast Cars")
This is the result you are looking for:
          
        
Credit: Tom Wright, affiliation unknown.
grid.newpage()
vp <- viewport(width=.5, height=.5) pushViewport(vp) grid.rect(gp=gpar(col=NA, fill="grey80"))
vp2 <- viewport(x=0, y=.5, width=.5, height=.5, just=c("left", "bottom")) pushViewport(vp2) grid.rect()
grid.newpage() pushViewport(vp) print(xyplot(mpg ~ disp, mtcars), newpage=FALSE)
The purpose of this exercise is to make use of the viewport() and pushViewport() functions.
The following code creates a 'lattice' scatterplot:
library(lattice) xyplot(mpg ~ disp, mtcars, main="Fast Cars")
This is the result you are looking for:
          
        
library(ggplot2) qplot(disp, mpg, data=mtcars, main="Fast Cars")
grid.ls()
layout
grid.force() grid.ls()
  layout
    background.1-7-10-1
    panel.6-4-6-4
      grill.gTree.537
        panel.background..rect.528
        panel.grid.minor.y..polyline.530
        panel.grid.minor.x..polyline.532
        panel.grid.major.y..polyline.534
        panel.grid.major.x..polyline.536
      NULL
      geom_point.points.524
grid.edit("title::text", grep=TRUE, gp=gpar(col="red"))
grid.remove("title.2-4-2-4")
qplot(disp, mpg, data=mtcars, main="Fast Cars") grid.force() grid.ls(viewports=TRUE, fullNames=TRUE)
  viewport[ROOT]
    viewport[layout]
      forcedgrob[layout]
        viewport[background.1-7-10-1]
          forcedgrob[background.1-7-10-1]
          upViewport[1]
        viewport[panel.6-4-6-4]
          forcedgrob[panel.6-4-6-4]
            gTree[grill.gTree.659]
              rect[panel.background..rect.650]
downViewport("panel.3-4-3-4") grid.rect(gp=gpar(col=NA, fill=rgb(0,1,0,.5)))
The purpose of this exercise is to make use of 'grid' functions with a 'ggplot2' plot.
The following code creates a 'ggplot2' scatterplot:
library(ggplot2) qplot(disp, mpg, data=mtcars, main="Fast Cars")
This is the result you are looking for:
          
        
library(vcd) mosaic(Titanic)
grid.ls()
GRID.lines.822 GRID.lines.823 disc:Class=1st,Sex=Male,Age=Child,Survived=No, circle:Class=1st,Sex=Male,Age=Child,Survived=No, rect:Class=1st,Sex=Male,Age=Child,Survived=Yes rect:Class=1st,Sex=Male,Age=Adult,Survived=No rect:Class=1st,Sex=Male,Age=Adult,Survived=Yes GRID.lines.824 GRID.lines.825 disc:Class=1st,Sex=Female,Age=Child,Survived=No,
plot(mpg ~ disp, mtcars, pch=16, main="Fast Cars") library(gridGraphics) grid.echo()
grid.ls()
graphics-background 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-main-1 graphics-plot-1-xlab-1 graphics-plot-1-ylab-1
grid.ls(viewports=TRUE, fullNames=TRUE)
  viewport[ROOT]
    rect[graphics-background]
    viewport[graphics-root]
      upViewport[1]
    downViewport[graphics-root]
      viewport[graphics-inner]
        upViewport[2]
    downViewport[graphics-root]
      downViewport[graphics-inner]
        viewport[graphics-figure-1]
The purpose of this exercise is to make use of 'grid' functions with a 'graphics' plot.
The following code creates a 'graphics' scatterplot:
plot(mpg ~ disp, mtcars, pch=16, main="Fast Cars")
This is the result you are looking for (before you delete the title):
          
        
 
       
      widths <- unit(c(1, 2, 1), c("null", "null", "cm")) lay <- grid.layout(3, 3, widths=widths) vplay <- viewport(layout=lay) pushViewport(vplay) pushViewport(viewport(layout.pos.row=2, layout.pos.col=2)) grid.rect(gp=gpar(col=NA, fill=rgb(1,0,0,.5)))
The purpose of this exercise is to make use of the grid.layout() function.
       +-----------------------------------------+ 
       |         w=7/9      5    h=0.5/5.5       |
       +-----+-----------------------------+-----+
       |     |              1    h=1.0/5.5 |     |
       |     +-----------------------------+     |
       |  6  |              2    h=1.0/5.5 |  7  |
       |     +-----------------------------+     |
       |w=1/9|              3    h=1.0/5.5 |w=1/9|
       |     +-----------------------------+     |
       |     |                             |     |
       |     |              4    h=2.0/5.5 |     |
       |     |                             |     |
       +-----+-----------------------------+-----+
        Credit: Julio Sergio Santana.
This is the result you are looking for (the grid.show.layout() function takes a 'grid' layout as its argument and draw a diagram of the layout):
          
        
grid.rect(width=stringWidth("axis label"))
grid.text("axis label", name="t") grid.rect(width=grobWidth("t"))
 
      grid.text("label", x=1/3, y=1/3, name="t") grid.circle(2/3, 2/3, r=unit(1, "mm"), gp=gpar(fill="black")) grid.segments(grobX("t", 0), grobY("t", 0), 2/3, 2/3)
The purpose of this exercise is to make use of the grobX(), grobY(), grobWidth(), and grobHeight() functions.
The following code draws two pieces of text:
grid.newpage() grid.text("label 1", 1/3, 2/3, name="l1") grid.text("label two", 2/3, 1/3, name="l2")
This is the result you are looking for:
          
        
 
      library(gridSVG) grid.circle(r=.2, name="c") grid.filter("c", filterEffect(feGaussianBlur(sd=3))) grid.export()
The purpose of this exercise is to make use of the 'gridSVG' package.
The following code draws a scatterplot:
library(lattice) xyplot(jitter(Sepal.Length) ~ jitter(Sepal.Width), group=Species, iris, par.settings=list( superpose.symbol=list(pch=21, col="black", fill="grey")))
          This is the result you are looking for
 (which is an
          SVG file):