### R code from vignette source 'murrell.Rnw'

###################################################
### code chunk number 1: murrell.Rnw:43-77
###################################################
library(gridGraphviz)
nodes <- c("grDevices", "grid", "graphics", 
           "lattice", "ggplot2", "othergrid",
           "plotrix", "maps", "othergraphics", 
           "SVG", "PNG", "PDF")
nn <- length(nodes)
labels <- nodes
labels[grepl("other", nodes)] <- "..."
names(labels) <- nodes
fills <- rep("white", nn)
fills[nodes %in% c("graphics", "grid")] <- "grey"
names(fills) <- nodes
edgeList <- list(grDevices=list(edges=c("PDF", "PNG", "SVG")),
                 graphics=list(edges="grDevices"),
                 grid=list(edges="grDevices"),
                 plotrix=list(edges="graphics"),
                 maps=list(edges="graphics"),
                 othergraphics=list(edges="graphics"),
                 lattice=list(edges="grid"),
                 ggplot2=list(edges="grid"),
                 othergrid=list(edges="grid"),
                 PDF=list(),
                 PNG=list(),
                 SVG=list())
gnel <- new("graphNEL", nodes=nodes, edgeL=edgeList, edgemode="directed")
rag <- agopenTrue(gnel, "system-graph",
                  attrs=list(node=list(shape="ellipse")),
                  nodeAttrs=list(label=labels, fillcolor=fills))
for (i in seq(along = AgEdge(rag))) {
    AgEdge(rag)[[i]]@arrowhead <- "normal"
}
pdf("murrell-system-graph.pdf", width=graphWidth(rag), height=graphHeight(rag))
grid.graph(rag)
dev.off()


###################################################
### code chunk number 2: plot
###################################################
plot(mpg ~ disp, mtcars, pch=16)


###################################################
### code chunk number 3: murrell.Rnw:107-108
###################################################
library(gridGraphics)


###################################################
### code chunk number 4: echo-src (eval = FALSE)
###################################################
## grid.echo()


###################################################
### code chunk number 5: echo
###################################################
f <- function() {
plot(mpg ~ disp, mtcars, pch=16)
}
grid.echo(f)


###################################################
### code chunk number 6: murrell.Rnw:147-148 (eval = FALSE)
###################################################
## grid.ls()


###################################################
### code chunk number 7: murrell.Rnw:151-154
###################################################
plot(mpg ~ disp, mtcars, pch=16)
f <- function() {
plot(mpg ~ disp, mtcars, pch=16)
}
grid.echo(f)
grid.ls()


###################################################
### code chunk number 8: editplot (eval = FALSE)
###################################################
## grid.edit("graphics-plot-1-bottom-axis-labels-1", rot=45, 
##           gp=gpar(col="red"))


###################################################
### code chunk number 9: murrell.Rnw:171-177
###################################################
pdf("murrell-editplot-%d.pdf", width=5, height=5, onefile=FALSE)
dev.control("enable")
plot(mpg ~ disp, mtcars, pch=16)
f <- function() {
plot(mpg ~ disp, mtcars, pch=16)
}
grid.echo(f)
grid.edit("graphics-plot-1-bottom-axis-labels-1", rot=45, 
          gp=gpar(col="red"))
dev.off()


###################################################
### code chunk number 10: coplot0
###################################################
coplot(lat ~ long | depth, quakes, pch=16, cex=.5,
       given.values=rbind(c(0, 400), c(300, 700)))


###################################################
### code chunk number 11: coplot
###################################################
coplot(lat ~ long | depth, quakes, pch=16, cex=.5,
       given.values=rbind(c(0, 400), c(300, 700)),
       bar.bg=c(num="white"))


###################################################
### code chunk number 12: coplot-echo (eval = FALSE)
###################################################
## grid.echo(prefix="cp")


###################################################
### code chunk number 13: coplot-edit (eval = FALSE)
###################################################
## grid.edit("cp-plot-4-box-1", gp=gpar(fill="red"))


###################################################
### code chunk number 14: murrell.Rnw:263-269
###################################################
pdf("murrell-coplot-edit-%d.pdf", onefile=FALSE)
dev.control("enable")
coplot(lat ~ long | depth, quakes, pch=16, cex=.5,
       given.values=rbind(c(0, 400), c(300, 700)),
       bar.bg=c(num="white"))
grid.echo(prefix="cp")
grid.edit("cp-plot-4-box-1", gp=gpar(fill="red"))
dev.off()


###################################################
### code chunk number 15: coplot-reorder (eval = FALSE)
###################################################
## gt <- grid.grab()
## gt <- reorderGrob(gt, "cp-plot-4-box-1")
## grid.newpage()
## grid.draw(gt)


###################################################
### code chunk number 16: murrell.Rnw:302-309
###################################################
pdf("murrell-coplot-reorder-%d.pdf", onefile=FALSE)
dev.control("enable")
coplot(lat ~ long | depth, quakes, pch=16, cex=.5,
       given.values=rbind(c(0, 400), c(300, 700)),
       bar.bg=c(num="white"))
grid.echo(prefix="cp")
grid.edit("cp-plot-4-box-1", gp=gpar(fill="red"))
gt <- grid.grab()
gt <- reorderGrob(gt, "cp-plot-4-box-1")
grid.newpage()
grid.draw(gt)
dev.off()


###################################################
### code chunk number 17: regions
###################################################
par(oma=rep(3, 4))
plot.new()
# set clipping to figure region
par(xpd=TRUE)
# deliberately draw a stupidly large rectangle
rect(-1, -1, 2, 2, col="gray80")
box("figure", lty="dashed")
# set clipping back to plot region
par(xpd=FALSE)
# deliberately draw a stupidly large rectangle
rect(-1, -1, 2, 2, col="white")
box("plot")
text(.5, .5, "Plot Region")
for (i in 1:4) {
    mtext(paste("Figure margin", i), side=i, line=1)
    mtext(paste("Outer margin", i), side=i, line=1, outer=TRUE)
}


###################################################
### code chunk number 18: murrell.Rnw:403-431 (eval = FALSE)
###################################################
## # Function to prettify the output of current.vpTree()
## formatVPTree <- function(x, indent=0) {
##     end <- regexpr("[)]+,?", x)
##     sibling <- regexpr(", ", x)
##     child <- regexpr("[(]", x)
##     if ((end < child || child < 0) && (end < sibling || sibling < 0)) {
##         lastchar <- end + attr(end, "match.length")
##         cat(paste0(paste(rep("  ", indent), collapse=""), 
##                    substr(x, 1, end - 1), "\n"))
##         if (lastchar < nchar(x)) {
##             formatVPTree(substring(x, lastchar + 1), 
##                          indent - attr(end, "match.length") + 1)
##         }
##     }
##     if (child > 0 && (sibling < 0 || child < sibling)) {
##         cat(paste0(paste(rep("  ", indent), collapse=""), 
##                    substr(x, 1, child - 3), "\n"))
##         formatVPTree(substring(x, child + 1), indent + 1)
##     }
##     if (sibling > 0 && sibling < end && (child < 0 || sibling < child)) {
##         cat(paste0(paste(rep("  ", indent), collapse=""), 
##                    substr(x, 1, sibling - 1), "\n"))
##         formatVPTree(substring(x, sibling + 2), indent)
##     }
## }
## plot(mpg ~ disp, mtcars, pch=16)
## f <- function() {
## plot(mpg ~ disp, mtcars, pch=16)
## }
## grid.echo(f)
## formatVPTree(current.vpTree())


###################################################
### code chunk number 19: murrell.Rnw:434-446
###################################################
library(gridDebug)
pdf("murrell-vptree-%d.pdf", width=17, height=7, onefile=FALSE)
plot(mpg ~ disp, mtcars, pch=16)
f <- function() {
plot(mpg ~ disp, mtcars, pch=16)
}
grid.echo(f)
rag <- gridTree(viewports=TRUE, grobs=FALSE, grid=TRUE, 
                vpNodeAttrs=list(shape="box", fontsize=20), 
                split=FALSE) 
for (i in seq(along = AgEdge(rag))) {
    AgEdge(rag)[[i]]@arrowhead <- "normal"
}
grid.graph(rag)
dev.off()


###################################################
### code chunk number 20: box (eval = FALSE)
###################################################
## downViewport("graphics-plot-1")
## grid.rect(gp=gpar(col="red", lwd=3))
## upViewport(0)


###################################################
### code chunk number 21: murrell.Rnw:477-482
###################################################
pdf("murrell-box-%d.pdf", width=5, height=5, onefile=FALSE)
plot(mpg ~ disp, mtcars, pch=16)
f <- function() {
plot(mpg ~ disp, mtcars, pch=16)
}
grid.echo(f)
downViewport("graphics-plot-1")
grid.rect(gp=gpar(col="red", lwd=3))
upViewport(0)
dev.off()


###################################################
### code chunk number 22: coplot-vp (eval = FALSE)
###################################################
## downViewport("cp-window-4-1")
## v <- unit(seq(0, 700, 100), "native")
## grid.segments(v, 0, v, 1, gp=gpar(col="red"), name="grid")
## upViewport(0)


###################################################
### code chunk number 23: coplot-vp-reorder (eval = FALSE)
###################################################
## gt <- grid.grab()
## gt <- reorderGrob(gt, "grid")
## grid.newpage()
## grid.draw(gt)


###################################################
### code chunk number 24: murrell.Rnw:527-534
###################################################
pdf("murrell-coplot-vp-reorder-%d.pdf", onefile=FALSE)
dev.control("enable")
coplot(lat ~ long | depth, quakes, pch=16, cex=.5,
       given.values=rbind(c(0, 400), c(300, 700)))
grid.echo(prefix="cp")
downViewport("cp-window-4-1")
v <- unit(seq(0, 700, 100), "native")
grid.segments(v, 0, v, 1, gp=gpar(col="red"), name="grid")
upViewport(0)
gt <- grid.grab()
gt <- reorderGrob(gt, "grid")
grid.newpage()
grid.draw(gt)
dev.off()


###################################################
### code chunk number 25: murrell.Rnw:569-573
###################################################
cpfun <- function() {
    coplot(lat ~ long | depth, quakes, pch=16, cex=.5,
           given.values=rbind(c(0, 400), c(300, 700)))
}


###################################################
### code chunk number 26: coplot-echofun (eval = FALSE)
###################################################
## grid.newpage()
## pushViewport(viewport(y=0, height=.7, just="bottom"))
## grid.echo(cpfun, newpage=FALSE, prefix="cp")
## upViewport()


###################################################
### code chunk number 27: murrell.Rnw:592-593
###################################################
library(ggplot2)


###################################################
### code chunk number 28: ggplot-hist (eval = FALSE)
###################################################
## pushViewport(viewport(y=1, height=.33, just="top"))
## gg <- ggplot(quakes) + geom_bar(aes(x=depth)) + 
##       theme(axis.title.x = element_blank())
## print(gg, newpage=FALSE)
## upViewport()


###################################################
### code chunk number 29: coplot-combined
###################################################
grid.newpage()
pushViewport(viewport(y=0, height=.7, just="bottom"))
grid.echo(cpfun, newpage=FALSE, prefix="cp")
upViewport()
pushViewport(viewport(y=1, height=.33, just="top"))
gg <- ggplot(quakes) + geom_bar(aes(x=depth)) + 
      theme(axis.title.x = element_blank())
print(gg, newpage=FALSE)
upViewport()
grid.rect(y=unit(1, "pt"), 
          width=unit(1, "npc") - unit(2, "pt"), 
          height=unit(.7, "npc") - unit(2, "pt"), 
          just="bottom", 
          gp=gpar(col="red", lty="dashed"))
dev.off()


###################################################
### code chunk number 30: murrell.Rnw:649-650
###################################################
library(gridSVG)


###################################################
### code chunk number 31: murrell.Rnw:652-654 (eval = FALSE)
###################################################
## plot(mpg ~ disp, mtcars, pch=16)
## grid.echo()


###################################################
### code chunk number 32: svg (eval = FALSE)
###################################################
## grid.garnish("graphics-plot-1-points-1", group=FALSE,
##              title=rownames(mtcars))
## grid.export("murrell-echo.svg")


###################################################
### code chunk number 33: murrell.Rnw:661-667
###################################################
pdf("murrell-echo-svg.pdf", onefile=FALSE)
dev.control("enable")
plot(mpg ~ disp, mtcars, pch=16)
f <- function() {
plot(mpg ~ disp, mtcars, pch=16)
}
grid.echo(f)
grid.garnish("graphics-plot-1-points-1", group=FALSE,
             title=rownames(mtcars))
grid.export("murrell-echo.svg")
dev.off()


###################################################
### code chunk number 34: murrell.Rnw:900-938
###################################################
library(gridGraphviz)
nodes <- c(nodes, "gridGraphics", "gridSVG")
nn <- length(nodes)
labels <- nodes
labels[grepl("other", nodes)] <- "..."
names(labels) <- nodes
cols <- rep("black", nn)
cols[nodes %in% "gridSVG"] <- "grey"
names(cols) <- nodes
fills <- rep("white", nn)
fills[nodes %in% c("graphics", "grid")] <- "grey"
fills[nodes %in% "gridGraphics"] <- "black"
names(fills) <- nodes
fontcols <- rep("black", nn)
fontcols[nodes %in% "gridGraphics"] <- "white"
fontcols[nodes %in% "gridSVG"] <- "grey"
names(fontcols) <- nodes
edgeList$graphics <- list(edges=c("grDevices", "gridGraphics"))
edgeList$grid <- list(edges=c("grDevices", "gridSVG"))
edgeList <- c(edgeList, 
              list(gridGraphics=list(edges="grid"),
                   gridSVG=list(edges="SVG")))
gnel <- new("graphNEL", nodes=nodes, edgeL=edgeList, edgemode="directed")
ne <- length(edgeNames(gnel))
edgecols <- rep("black", ne)
names(edgecols) <- edgeNames(gnel)
edgecols[edgeNames(gnel) %in% c("grid~gridSVG", "gridSVG~SVG")] <- "grey"
rag <- agopenTrue(gnel, "gridGraphics",
                  attrs=list(node=list(shape="ellipse")),
                  nodeAttrs=list(label=labels, fillcolor=fills, color=cols,
                      fontcolor=fontcols),
                  edgeAttrs=list(color=edgecols))
for (i in seq(along = AgEdge(rag))) {
    AgEdge(rag)[[i]]@arrowhead <- "normal"
}
pdf("murrell-gridGraphics.pdf", width=graphWidth(rag), height=graphHeight(rag))
grid.graph(rag)
dev.off()


