``````## Chapter 15: Graphs in R

## Sec 15.1: Hardcopy graphics devices
library(DAAG)``````
``## Loading required package: lattice``
``````pdf(file="fossilfuel.pdf", width=6.5, height=6.5, pointsize=18)
# For pdf() and postscript(), heights and widths are in inches
plot(carbon ~ year, data=fossilfuel, pch=16)
dev.off() ``````
``````## pdf
##   2``````
``````par(pty="s")
plot(carbon ~ year, data=fossilfuel, pch=16)  # Display graph ``````

``````par(pty="m")
## Now open pdf device and copy graph to it
dev.copy(pdf, file="fossilcopy.pdf") ``````
``````## pdf
##   3``````
``dev.off()            # Close pdf device ``
``````## pdf
##   2``````
``````## Sec 15.2: Plotting characters, symbols, line types and colors
##                           Font families
##                               Colors
##                  Contour and filled contour plots

## Sec 15.3: Formatting and plotting of text and equations
## ss 15.3.1: Symbolic substitution of symbols in an expression
library(DAAG)
## Replace the symbol tx by its value
specnam <- "Acmena smithii"
plot(wood ~ dbh, data=rainforest, subset=species==specnam)
title(main=substitute(italic(tx) * ": " * "wood vs dbh",
list(tx=specnam))) ``````

``````## ss 15.3.2: Plotting expressions in parallel
## Sample from bivariate normal (x, y) with correlation rho
rho <- 0.75; x <- rnorm(40); y <- rnorm(40) + rho/sqrt(1-rho^2)*x
## Now map all values of x onto 3 integer values
x0 <- cut(x, breaks=c(-Inf,-1,1,Inf))
plot(unclass(x0), y, xaxt="n")
axis(3, at=1:3, labels=expression("(-Inf,-1]", "(-1,1]", "(1, Inf]"))

tiklab <- lapply(strsplit(levels(x0), "Inf"),
function(x)if(length(x)>1)substitute(x1*infinity*x2,
list(x1=x[1],x2=x[2])) else x)
axis(1, at=1:3, labels=as.expression(tiklab)) ``````

``````##                 Symbolic substitution in parallel
seps <- c(-12.5, -10.523, -8.523, -6.85, -6.6, -3.523, -1, 1, 2.5)
plot(range(10^seps), c(0, 1), axes=FALSE, xlab="Wavelength (m)",
ylab="", type="n", xaxs="i", yaxs="i", log="x")
log10ticks <- pretty(seps,6)
log10ticks <- log10ticks[log10ticks > seps[1]]
ticklabs <- lapply(round(log10ticks,2),
function(x)substitute(10^a, list(a=x)))
axis(1, at=10^log10ticks, labels=as.expression(ticklabs))
## Use rect() to divide the axis a/c to types of radiation
len <- length(seps)
rect(xleft=10^seps[-len], ybottom=rep(0, len-1),
xright=10^seps[-1], ytop=rep(1, len-1),
border=c(NA, rep(1,len-3), NA),
col=c(rep("gray70",3), "white", rep("gray70",4)))
mid <- 0.5*(seps[-1]+seps[-len])

``````##                  A further use for {substitute()}
testfun <- function(x, y)deparse(substitute(x))
testfun(x = AnyValidName) ``````
``## [1] "AnyValidName"``
``````## Sec 15.4: Multiple graphs on a single graphics page
par(fig = c(0, 1, 0.38, 1))  # xleft, xright, ybottom, ytop
## Plot graph A
par(fig = c(0, 1, 0, 0.38), new=TRUE)
## Plot graph B
par(fig = c(0, 1, 0, 1))     # Reset to default

library(lattice)
cuckoos.strip <- stripplot(species ~ length, xlab="", data=cuckoos)
print(cuckoos.strip, position=c(0,0.5,1,1))
# x, y, x, y; left, bottom, right, top
cuckoos.bw <- bwplot(species ~ length, xlab="", data=cuckoos)
print(cuckoos.bw, position=c(0,0,1,0.5), newpage=FALSE)
# NB: Use newpage=FALSE in order to retain the first graph

##          Base and trellis plots on the same graphics page
par(new=FALSE)``````

``````plot(0:1, 0:1, type="n", bty="n", axes=FALSE, xlab="", ylab="")
mtext(side=3, line=3, "Lattice bwplot (i.e., boxplot)")
cuckoos.bw <- bwplot(species~length, data=cuckoos)
print(cuckoos.bw, newpage=FALSE) ``````

``````##              Use of {layout()} -- base graphics only

## Sec 15.5: Lattice Graphics, and the {grid} Package
##                 Lattice Graphics vs Base Graphics
## ss 15.5.1: Groups within data, and/or columns in parallel
## Simple version of plot
grogplot <- xyplot(Beer+Spirit+Wine ~ Year | Country, data=grog,
aspect=1, outer=FALSE, auto.key=list(columns=3))
## Enhance, and print enhanced code
update(grogplot, ylim=c(0,5.5),
xlab="", ylab="Amount consumed (per person)",
par.settings=simpleTheme(pch=c(1,3,4))) ``````