library(lattice)
xyplot(mpg ~ disp, mtcars)
svg("lattice-plot.svg")
print(xyplot(mpg ~ disp, mtcars))
dev.off()
library(grid)
grid.ls()
print(xyplot(mpg ~ disp, mtcars))
library(grid)
grid.ls()
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)
svg("lattice-plot-viewports.svg")
print(xyplot(mpg ~ disp, mtcars))
annotate <- function(vp) {
downViewport(vp)
grid.rect(gp=gpar(col=NA, fill=rgb(0,1,0,.5)))
upViewport()
}
annotate("plot_01.xlab.vp")
annotate("plot_01.ylab.vp")
annotate("plot_01.panel.1.1.vp")
dev.off()
grid.ls(viewports=TRUE, grobs=FALSE)
print(xyplot(mpg ~ disp, mtcars))
temp <- grid.ls(viewports=TRUE, grobs=FALSE, print=FALSE)
vpls <- lapply(temp, "[",
rev(rev(which(temp$type == "vpListing"))[-1]))
class(vpls) <- class(temp)
vpls
grid.ls(viewports=TRUE, fullNames=TRUE)
print(xyplot(mpg ~ disp, mtcars))
temp <- grid.ls(viewports=TRUE, fullNames=TRUE, print=FALSE)
fullls <- lapply(temp, "[", 1:10)
class(fullls) <- class(temp)
fullls
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(grid) grid.ls() # plot_01.main
library(grid)
grid.ls(viewports=TRUE, fullNames=TRUE)
# plot_01.main.vp
# note the encoding to get the macrons
donations <- read.csv("Data/electoral_donations_2014.csv", encoding="UTF-8")
# biggest donations
head(donations[order(donations$Amount_Donated, decreasing=TRUE), ])
# Totals by party
totals <- aggregate(donations["Amount_Donated"],
list(Party=donations$Party),
sum)
sortedTotals <- totals[order(totals$Amount_Donated, decreasing=TRUE), ]
sortedTotals$Party <- factor(sortedTotals$Party,
levels=rev(sortedTotals$Party))
pdf(NULL, width=7, height=5, pointsize=12)
dev.control("enable")
library(lattice)
barchart(Party ~ Amount_Donated, sortedTotals,
origin=0,
xlab="Total Donated",
col=c("darkblue", "red2", "purple", "red2", "black", "lightblue",
"yellow", "white", "white", "yellow", "darkgreen", "grey",
"white"),
border=c("darkblue", "red2", "purple", "black", "red", "lightblue",
"blue", "black", "lightblue", "green", "darkgreen", "grey",
"purple"),
lwd=3,
panel=function(...) {
panel.abline(v=c(0, 500000, 1000000), col="grey")
panel.barchart(...)
})
t <- grid.get("ticklabels.bottom", grep=TRUE)
barchartGrobs <- grid.ls(print=FALSE)
library(gridSVG)
grid.export("lattice-barchart.svg", prefix="lattice-barchart")
library(grid)
grid.remove("border", grep=TRUE)
grid.export("lattice-barchart-mod.svg", prefix="lattice-barchart-mod")
grid.edit("ticklabels.bottom", just=c("left", "top"), grep=TRUE)
grid.export("lattice-barchart-edit.svg", prefix="lattice-barchart-edit")
grid.edit("ticklabels.bottom", gp=gpar(col="grey"), grep=TRUE)
grid.export("lattice-barchart-edit-gp.svg", prefix="lattice-barchart-edit-gp")
dev.off()
library(lattice)
barchart(Party ~ Amount_Donated, sortedTotals)
library(grid)
grid.ls()
barchartGrobs
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)
names(t)
t$just
library(grid)
grid.edit("plot_01.ticklabels.bottom.panel.1.1",
just=c("left", "top"))
library(grid)
gpar(col="blue", lwd=3, lty="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")
library(grid)
grid.edit("plot_01.main", gp=gpar(col="red"))
library(grid)
grid.remove("plot_01.main")
This is the result you are looking for (before you remove the title):
svg("lattice-remove-grob.svg")
library(lattice)
xyplot(mpg ~ disp, mtcars, main="Fast Cars")
library(grid)
grid.edit("plot_01.main", gp=gpar(col="red"))
dev.off()
Credit: John G. Bullock, Yale University.
Credit: John G. Bullock, Yale University.
xyplot(mpg ~ disp, mtcars)
grid.ls(viewports=TRUE, fullNames=TRUE)
print(xyplot(mpg ~ disp, mtcars))
temp <- grid.ls(viewports=TRUE, fullNames=TRUE, print=FALSE)
fullls <- lapply(temp, "[", 1:10)
class(fullls) <- class(temp)
fullls
svg("lattice-plot-vp-tree.svg", width=10, height=5)
library(gridGraphviz)
vplsshort <- lapply(vpls, "[", 1:6)
class(vplsshort) <- class(vpls)
nodes <- vplsshort$name
edges <- rep(list(list()), 6)
names(edges) <- nodes
edges$ROOT <- list(edges="plot_01.toplevel.vp")
edges$plot_01.toplevel.vp <- list(edges=nodes[-(1:2)])
gnel <- new("graphNEL", nodes, edges, edgemode="directed")
rag <- agopenTrue(gnel, "",
attrs=list(node=list(shape="ellipse")))
# grid.newpage()
grid.graph(rag)
dev.off()
downViewport("plot_01.toplevel.vp")
grid.rect(gp=gpar(col=NA, fill=rgb(0,1,0,.5)))
svg("lattice-plot-down-viewport.svg")
xyplot(mpg ~ disp, mtcars)
downViewport("plot_01.toplevel.vp")
grid.rect(gp=gpar(col=NA, fill=rgb(0,1,0,.5)))
dev.off()
downViewport("plot_01.panel.1.1.vp")
grid.rect(gp=gpar(col=NA, fill=rgb(1,0,0,.5)))
svg("lattice-plot-down-2-viewport.svg")
xyplot(mpg ~ disp, mtcars)
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)))
dev.off()
upViewport()
grid.rect(gp=gpar(col=NA, fill=rgb(0,0,1,.5)))
svg("lattice-plot-up-viewport.svg")
xyplot(mpg ~ disp, mtcars)
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)))
dev.off()
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")
library(grid)
downViewport("plot_01.main.vp")
grid.rect()
This is the result you are looking for:
svg("lattice-down-viewport.svg")
library(lattice)
xyplot(mpg ~ disp, mtcars, main="Fast Cars")
library(grid)
downViewport("plot_01.main.vp")
grid.rect(gp=gpar(col=NA, fill=rgb(0,0,1,.5)))
dev.off()
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"))
svg("lattice-plot-vp-coords-native.svg")
xyplot(mpg ~ disp, mtcars)
downViewport("plot_01.panel.1.1.vp")
grid.lines(unit(c(0, 300, 300), c("npc", "native", "native")),
unit(c(30, 30, 0), c("native", "native", "npc")),
gp=gpar(lty="dashed", lwd=2))
grid.text("native", x=unit(300, "native"), y=unit(30, "native"),
just=c("left", "bottom"))
dev.off()
grid.text("absolute",
x=unit(1, "in"), y=unit(1, "cm"),
just=c("left", "bottom"))
svg("lattice-plot-vp-coords-abs.svg")
xyplot(mpg ~ disp, mtcars)
downViewport("plot_01.panel.1.1.vp")
grid.lines(unit(c(0, 1, 1), c("npc", "in", "in")),
unit(c(1, 1, 0), c("cm", "cm", "npc")),
gp=gpar(lty="dashed", lwd=2))
grid.text("absolute",
x=unit(1, "in"), y=unit(1, "cm"),
just=c("left", "bottom"))
dev.off()
grid.text("normalised",
x=unit(.75, "npc"), y=unit(.5, "npc"),
just=c("left", "bottom"))
svg("lattice-plot-vp-coords-npc.svg")
xyplot(mpg ~ disp, mtcars)
downViewport("plot_01.panel.1.1.vp")
grid.lines(unit(c(0, .75, .75), "npc"),
unit(c(.5, .5, 0), "npc"),
gp=gpar(lty="dashed", lwd=2))
grid.text("normalised",
x=unit(.75, "npc"), y=unit(.5, "npc"),
just=c("left", "bottom"))
dev.off()
grid.text("normalised - absolute",
x=unit(1, "npc") - unit(1, "cm"),
y=unit(1, "npc") - unit(1, "cm"),
just=c("right", "top"))
svg("lattice-plot-vp-coords-npc-abs.svg")
xyplot(mpg ~ disp, mtcars)
downViewport("plot_01.panel.1.1.vp")
grid.move.to(unit(1, "npc") - unit(1, "cm"), 1)
grid.line.to(unit(1, "npc") - unit(1, "cm"),
unit(1, "npc") - unit(1, "cm"),
gp=gpar(lty="dashed", lwd=2))
grid.line.to(1, unit(1, "npc") - unit(1, "cm"),
gp=gpar(lty="dashed", lwd=2))
grid.text("normalised - absolute",
x=unit(1, "npc") - unit(1, "cm"),
y=unit(1, "npc") - unit(1, "cm"),
just=c("right", "top"))
dev.off()
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"))
svg("multiple-units.svg", width=3, height=3)
# grid.newpage()
grid.segments(1:4/5, 0, 1:4/5, 1, gp=gpar(col="grey"))
grid.text(0:4/5,
x=unit(0:4/5, "npc") + unit(1, "mm"),
y=unit(1, "npc") - unit(1, "mm"),
just=c("left", "top"),
gp=gpar(col="grey", cex=.5))
grid.segments(0, unit(seq(50, 200, 50), "native"),
1, unit(seq(50, 200, 50), "native"),
gp=gpar(col="grey"))
grid.text(seq(0, 200, 50),
x=unit(1, "npc") - unit(2, "mm"),
y=unit(seq(0, 200, 50), "native") - unit(1, "mm"),
just=c("right", "top"),
gp=gpar(col="grey", cex=.5))
grid.segments(0, .2, 1, .2,
arrow=arrow(type="closed", ends="both",
angle=15, length=unit(3, "mm")),
gp=gpar(col="grey", fill="grey", lwd=3))
tg <- textGrob("3 inches", y=.2, gp=gpar(cex=.5, col="grey"))
grid.rect(y=.2, width=1.2*grobWidth(tg), height=unit(1, "lines"),
gp=gpar(col=NA, fill="white"))
grid.draw(tg)
grid.rect(x=unit(.2, "npc"), y=unit(100, "native"),
width=unit(1, "in"), height=unit(1, "lines"))
grid.text("text", x=unit(.2, "npc"), y=unit(100, "native"),
gp=gpar(col="grey"))
dev.off()
grid.circle(x=1:5/6, y=.5, r=1:5/25)
svg("vectorised-grobs.svg")
grid.circle(x=1:5/6, y=.5, r=1:5/20, gp=gpar(fill=NA))
dev.off()
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")
library(grid)
downViewport("plot_01.panel.1.1.vp")
grid.segments(0, unit(25, "native"),
1, unit(25, "native"))
upViewport(0)
library(grid)
downViewport("plot_01.panel.1.1.vp")
grid.text("Pontiac Firebird",
x=unit(400, "native") + unit(1, "mm"),
y=unit(19.2, "native") + unit(1, "mm"),
just=c("left", "bottom"))
upViewport(0)
This is the result you are looking for:
svg("lattice-viewport-add.svg")
library(lattice)
xyplot(mpg ~ disp, mtcars, main="Fast Cars")
library(grid)
downViewport("plot_01.panel.1.1.vp")
grid.segments(0, unit(25, "native"),
1, unit(25, "native"))
grid.text("Pontiac Firebird",
x=unit(400, "native") + unit(1, "mm"),
y=unit(19.2, "native") + unit(1, "mm"),
just=c("left", "bottom"))
upViewport(0)
dev.off()
library(grid)
library(lattice)
data_macular <- read.csv('Data/anonymous_data.csv', row.names=1)
# Reduce down to four genes for demonstration purposes
data_macular <-
data_macular[as.numeric(gsub("Gene", "", data_macular$gene)) < 5, ]
data_macular$gene <- factor(data_macular$gene)
genes <- levels(data_macular$gene)
# The problem (too many empty panels)
svg("lattice-empty-panels.svg")
print(xyplot(rnflThickness ~ age | mrn + gene, data_macular, pch=16,
par.strip.text=list(cex=.5)))
dev.off()
# A solution with grid viewports
xlim <- extendrange(data_macular$age)
ylim <- extendrange(data_macular$rnflThickness)
highlights <- hcl(1:4*90, 60, 60)
highlights <- adjustcolor(highlights, alpha=.5)
vpplot <- function(highlight=FALSE) {
# grid.newpage()
pushViewport(viewport(width=.9, height=.9,
layout=grid.layout(4, 1)))
for (i in seq_along(genes)) {
pushViewport(viewport(layout.pos.row=5 - i))
print(xyplot(rnflThickness ~ age | mrn, data_macular, pch=16,
subset=gene == genes[i],
layout=c(6, 1),
xlim=xlim, ylim=ylim, ylab=NULL, xlab=NULL,
scales=list(x=list(alternating=rep(1, 6), labels=i == 1))),
newpage=FALSE,
panel.width=list(2, "cm"), panel.height=list(2, "cm"))
grid.rect(x=0, width=unit(2, "lines"), height=.8, just="left",
gp=gpar(col=NA, fill="grey"))
grid.text(genes[i], x=unit(1, "lines"), rot=90)
if (highlight) {
grid.rect(gp=gpar(col=NA, fill=highlights[i]))
}
upViewport()
}
grid.text("retinal nerve fiber thickness", x=unit(-1, "lines"), rot=90)
grid.text("age", y=0, just="top")
}
svg("lattice-multi-plots.svg")
vpplot()
dev.off()
svg("lattice-multi-plots-vp.svg")
vpplot(TRUE)
dev.off()
Credit: Tom Wright, affiliation unknown.
grid.newpage()
svg("grid-newpage.svg")
grid.newpage()
dev.off()
vp <- viewport(width=.5, height=.5)
pushViewport(vp)
grid.rect(gp=gpar(col=NA, fill="grey80"))
svg("grid-viewport.svg")
vp <- viewport(width=.5, height=.5)
pushViewport(vp)
grid.rect(gp=gpar(col=NA, fill="grey80"))
dev.off()
vp2 <- viewport(x=0, y=.5, width=.5, height=.5,
just=c("left", "bottom"))
pushViewport(vp2)
grid.rect()
svg("grid-viewport2.svg")
vp2 <- viewport(x=0, y=.5, width=.5, height=.5,
just=c("left", "bottom"))
pushViewport(vp)
grid.rect(gp=gpar(col=NA, fill="grey80"))
pushViewport(vp2)
grid.rect()
dev.off()
grid.newpage()
pushViewport(vp)
print(xyplot(mpg ~ disp, mtcars), newpage=FALSE)
svg("grid-viewport-lattice-plot.svg")
pushViewport(vp)
grid.rect(gp=gpar(col=NA, fill="grey80"))
print(xyplot(mpg ~ disp, mtcars), newpage=FALSE)
dev.off()
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")
library(grid)
grid.newpage()
pushViewport(viewport(x=.5, y=0, width=.5, height=.5,
just=c("left", "bottom")))
print(xyplot(mpg ~ disp, mtcars, main="Fast Cars"),
newpage=FALSE)
upViewport()
This is the result you are looking for:
svg("grid-viewport-create.svg")
library(grid)
grid.newpage()
pushViewport(viewport(x=.5, y=0, width=.5, height=.5,
just=c("left", "bottom")))
print(xyplot(mpg ~ disp, mtcars, main="Fast Cars"),
newpage=FALSE)
upViewport()
dev.off()
library(ggplot2)
qplot(disp, mpg, data=mtcars, main="Fast Cars")
svg("ggplot2-plot.svg")
print(qplot(disp, mpg, data=mtcars, main="Fast Cars"))
dev.off()
grid.ls()
print(qplot(disp, mpg, data=mtcars, main="Fast Cars"))
grid.ls()
grid.force()
grid.ls()
print(qplot(disp, mpg, data=mtcars, main="Fast Cars"))
grid.force()
temp <- grid.ls(print=FALSE)
grobls <- lapply(temp, "[", 1:11)
class(grobls) <- class(temp)
grobls
grid.edit("title::text", grep=TRUE,
gp=gpar(col="red"))
pdf(NULL)
dev.control("enable")
print(qplot(disp, mpg, data=mtcars, main="Fast Cars"))
grid.force()
grid.edit("title::text", grep=TRUE, gp=gpar(col="red"))
grid.export("ggplot2-plot-edit.svg", prefix="ggplot2-plot-edit")
grid.remove("title.2-4-2-4")
grid.export("ggplot2-plot-rm.svg", prefix="ggplot2-plot-rm")
dev.off()
grid.remove("title.2-4-2-4")
qplot(disp, mpg, data=mtcars, main="Fast Cars")
grid.force()
grid.ls(viewports=TRUE, fullNames=TRUE)
print(qplot(disp, mpg, data=mtcars, main="Fast Cars"))
grid.force()
temp <- grid.ls(viewports=TRUE, fullNames=TRUE, print=FALSE)
fullls <- lapply(temp, "[", 1:10)
class(fullls) <- class(temp)
fullls
downViewport("panel.3-4-3-4")
grid.rect(gp=gpar(col=NA, fill=rgb(0,1,0,.5)))
svg("ggplot2-plot-down-viewport.svg")
qplot(disp, mpg, data=mtcars, main="Fast Cars")
grid.force()
downViewport("panel.6-4-6-4")
grid.rect(gp=gpar(col=NA, fill=rgb(0,1,0,.5)))
dev.off()
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")
grid.force()
downViewport("title.2-4-2-4")
grid.rect(gp=gpar(col=NA, fill=rgb(0,0,1,.5)), name="r")
grid.edit("title::text", grep=TRUE, x=unit(1, "npc"), hjust=1)
# MUCH easier if you gave the grob a name in the previous answer!
grid.remove("r")
This is the result you are looking for:
svg("ggplot2-plot-ann.svg")
library(ggplot2)
qplot(disp, mpg, data=mtcars, main="Fast Cars")
grid.force()
library(grid)
downViewport("title.2-4-2-4")
grid.edit("title::text", grep=TRUE, x=unit(1, "npc"), hjust=1)
dev.off()
library(vcd)
mosaic(Titanic)
svg("vcd-plot.svg")
library(vcd)
mosaic(Titanic)
vcdls <- capture.output(grid.ls())
dev.off()
grid.ls()
cat(head(vcdls, 10), sep="\n")
plot(mpg ~ disp, mtcars, pch=16, main="Fast Cars")
library(gridGraphics)
grid.echo()
svg("gridGraphics-plot.svg")
dev.control("enable")
plot(mpg ~ disp, mtcars, pch=16, main="Fast Cars")
library(gridGraphics)
grid.echo()
gridgraphicsls <- grid.ls(print=FALSE)
temp <- grid.ls(viewports=TRUE, fullNames=TRUE, print=FALSE)
fullls <- lapply(temp, "[", 1:10)
class(fullls) <- class(temp)
gridgraphicsvp <- fullls
dev.off()
grid.ls()
gridgraphicsls
grid.ls(viewports=TRUE, fullNames=TRUE)
print(gridgraphicsvp)
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")
plot(mpg ~ disp, mtcars, pch=16, main="Fast Cars")
library(gridGraphics)
grid.echo()
grid.edit("graphics-plot-1-main-1", gp=gpar(col="red"))
plot(mpg ~ disp, mtcars, pch=16, main="Fast Cars")
library(gridGraphics)
grid.echo()
grid.remove("graphics-plot-1-main-1")
This is the result you are looking for (before you delete the title):
svg("graphics-plot.svg")
dev.control("enable")
plot(mpg ~ disp, mtcars, pch=16, main="Fast Cars")
library(gridGraphics)
grid.echo()
grid.edit("graphics-plot-1-main-1", gp=gpar(col="red"))
dev.off()
svg("lattice.svg")
library(lattice)
barchart(yield ~ variety | site, data = barley,
groups = year, layout = c(1,6), stack = TRUE,
auto.key = list(space = "right"),
ylab = "Barley Yield (bushels/acre)",
scales = list(x = list(rot = 45)))
dev.off()
svg("ggplot2.svg")
library(ggplot2)
model <- lm(mpg ~ wt + factor(cyl), data=mtcars)
grid <- with(mtcars, expand.grid(wt = seq(min(wt), max(wt), length = 20),
cyl = levels(factor(cyl))))
grid$mpg <- stats::predict(model, newdata=grid)
err <- stats::predict(model, newdata=grid, se = TRUE)
grid$fit <- err$fit
grid$ucl <- err$fit + 1.96 * err$se.fit
grid$lcl <- err$fit - 1.96 * err$se.fit
qplot(wt, mpg, data=mtcars, colour=factor(cyl)) +
geom_line(aes(y=fit), data=grid) +
geom_ribbon(aes(ymin = lcl, ymax = ucl), data=grid, alpha=.1, lwd=0)
dev.off()
svg("lattice-layout.svg")
barchart(yield ~ variety | site, data = barley,
groups = year, layout = c(1,6), stack = TRUE,
auto.key = list(space = "right"),
ylab = "Barley Yield (bushels/acre)",
scales = list(x = list(rot = 45)))
grid.rect(gp=gpar(col=NA, fill=rgb(1,1,1,.5)))
downViewport("plot_01.toplevel.vp")
nr <- current.viewport()$layout$nrow
nc <- current.viewport()$layout$ncol
for (i in 1:nr) {
for (j in 1:nc) {
pushViewport(viewport(layout.pos.col=j, layout.pos.row=i))
grid.rect(gp=gpar(col="red", fill=NA))
popViewport()
}
}
dev.off()
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)))
svg("grid-layout.svg", width=3, height=3)
pushViewport(vplay)
for (i in 1:3) {
pushViewport(viewport(layout.pos.row=i))
grid.segments(0, 0, 1, 0, gp=gpar(lty="dashed", lwd=2))
if (i == 1)
grid.segments(0, 1, 1, 1, gp=gpar(lty="dashed", lwd=2))
popViewport()
}
for (j in 1:3) {
pushViewport(viewport(layout.pos.col=j))
grid.segments(0, 0, 0, 1, gp=gpar(lty="dashed", lwd=2))
if (i == 3)
grid.segments(1, 0, 1, 1, gp=gpar(lty="dashed", lwd=2))
popViewport()
}
pushViewport(viewport(layout.pos.row=2, layout.pos.col=2))
grid.rect(gp=gpar(col=NA, fill=rgb(1,0,0,.5)))
dev.off()
The purpose of this exercise is to make use of the grid.layout() function.
plot(mpg ~ disp, mtcars, pch=16, main="Fast Cars")
library(gridGraphics)
grid.echo()
grid.edit("graphics-plot-1-main-1", gp=gpar(col="red"))
+-----------------------------------------+
| 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.
layout <- grid.layout(5, 3, widths=c(1, 7, 1),
heights=c(.5, 1, 1, 1, 2))
grid.show.layout(layout)
grid.newpage()
pushViewport(viewport(layout=layout))
pushViewport(viewport(layout.pos.row=1))
grid.rect(); grid.text("5")
popViewport()
pushViewport(viewport(layout.pos.row=2, layout.pos.col=2))
grid.rect(); grid.text("1")
popViewport()
pushViewport(viewport(layout.pos.row=3, layout.pos.col=2))
grid.rect(); grid.text("2")
popViewport()
pushViewport(viewport(layout.pos.row=4, layout.pos.col=2))
grid.rect(); grid.text("3")
popViewport()
pushViewport(viewport(layout.pos.row=5, layout.pos.col=2))
grid.rect(); grid.text("4")
popViewport()
pushViewport(viewport(layout.pos.row=2:5, layout.pos.col=1))
grid.rect(); grid.text("6")
popViewport()
pushViewport(viewport(layout.pos.row=2:5, layout.pos.col=3))
grid.rect(); grid.text("7")
popViewport()
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):
svg("grid-show-layout.svg")
layout <- grid.layout(5, 3, widths=c(1, 7, 1),
heights=c(.5, 1, 1, 1, 2))
grid.show.layout(layout)
dev.off()
grid.rect(width=stringWidth("axis label"))
svg("grid-stringwidth.svg", width=2, height=2)
grid.text("axis label", gp=gpar(col="grey"))
grid.rect(width=stringWidth("axis label"), gp=gpar(fill=NA))
dev.off()
grid.text("axis label", name="t")
grid.rect(width=grobWidth("t"))
svg("grid-grobwidth.svg", width=2, height=2)
grid.text("axis label", name="t")
grid.rect(width=grobWidth("t"), gp=gpar(fill=NA))
dev.off()
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)
svg("grid-grobx.svg", width=3, height=3)
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)
dev.off()
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")
grid.circle(1/3, 2/3, r=0.5*grobWidth("l1") + unit(1, "mm"),
name="c1")
grid.circle(2/3, 1/3, r=0.5*grobWidth("l2") + unit(1, "mm"),
name="c2")
grid.segments(grobX("c1", 315), grobY("c1", 315),
grobX("c2", 135), grobY("c2", 135))
This is the result you are looking for:
svg("grob-width-x.svg", width=3, height=3)
grid.text("label 1", 1/3, 2/3, name="l1")
grid.text("label two", 2/3, 1/3, name="l2")
grid.circle(1/3, 2/3, r=0.5*grobWidth("l1") + unit(1, "mm"),
name="c1", gp=gpar(fill=NA))
grid.circle(2/3, 1/3, r=0.5*grobWidth("l2") + unit(1, "mm"),
name="c2", gp=gpar(fill=NA))
grid.segments(grobX("c1", 315), grobY("c1", 315),
grobX("c2", 135), grobY("c2", 135))
dev.off()
# Background data
unknown <- 8.7
total <- 9.1
known <- total - unknown
theta0 <- pi/4
thetaN <- theta0 + 2*pi*unknown/total
theta <- seq(theta0, thetaN, length.out=100)
x <- 0.3*c(0, cos(theta)) + 0.5
y <- 0.3*c(0, sin(theta)) + 0.35
library(png)
bg <- readPNG("AfterTheBombs.png")
library(gridSVG)
gridsvg("gridsvg.svg", width=7, height=5.5, res=96, prefix="gridsvg")
# grid.newpage()
grid.raster(bg, height=1)
pushViewport(viewport(width=unit(1, "snpc"), height=unit(1, "snpc")))
grid.polygon(x, y, gp=gpar(col=NA, fill=rgb(.67, 0, .11)),
name="pie")
# This could, in theory, be done with an feBlend filter
# using "BackgroundImage" as second source, but that
# requires setting 'enable-background;"new"' somewhere in
# the SVG (at least), which is not (currently) possible
# with 'gridSVG'
img <- feImage(href="AfterTheBombs-flipped.png",
preserveAspectRatio="xMidYMin slice",
result="img")
compose <- feBlend("SourceGraphic", "img", "darken")
grid.filter("pie", filterEffect(list(img, compose)))
headText <-
"The Department of Defense is unable to account for the use of
$8.7 billion of the $9.1 billion it spent on reconstruction in Iraq"
heading <- textGrob(headText,
x=unit(0.5, "cm"),
y=unit(3, "lines"),
just=c("left", "top"),
gp=gpar(col="white"))
pushViewport(viewport(gp=gpar(cex=.8)),
viewport(x=0.05, y=1,
just=c("left", "top"),
height=grobHeight(heading) + unit(4, "lines"),
width=grobWidth(heading) + unit(1, "cm")))
grid.rect(gp=gpar(fill="black"))
grid.segments(x0=unit(0.5, "cm"),
x1=unit(1, "npc") - unit(0.5, "cm"),
y0=unit(1, "npc") - unit(2, "lines"),
y1=unit(1, "npc") - unit(2, "lines"),
gp=gpar(col="grey50", lwd=2))
grid.text("That's 96 Percent",
x=unit(0.5, "cm"),
y=unit(1, "npc") - unit(1, "lines"),
just="left",
gp=gpar(fontface="bold", col="white"))
grid.draw(heading)
popViewport(2)
dev.off()
library(gridSVG)
grid.circle(r=.2, name="c")
grid.filter("c", filterEffect(feGaussianBlur(sd=3)))
grid.export()
library(gridSVG)
gridsvg("gridsvg-blur.svg", prefix="gridsvg-blur")
grid.circle(r=.2, name="c")
grid.filter("c", filterEffect(feGaussianBlur(sd=3)))
dev.off()
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")))
library(gridSVG)
grid.filter("group.[12]", grep=TRUE, global=TRUE,
filterEffect(feGaussianBlur(sd=3)))
grid.export("lattice-iris-blur.svg")
This is the result you are looking for
(which is an
SVG file):