`g1.1` <- function(device=""){ if(device!="")hardcopy(width=2.25, height=2.25, device=device) oldpar <- par(mar = c(4.1,4.1,1.1,1.1), pty="s", mgp=c(2.5,0.75,0)) on.exit(par(oldpar)) Year <- c(1800, 1850, 1900, 1950, 2000) Carbon <- c(8, 54, 534, 1630, 6611) plot(Carbon ~ Year, pch=16) if(device!="")dev.off() } `g1.2` <- function(device=""){ if(device!="")hardcopy(device=device, width=2.3, height=2.3) oldpar <- par(mar = c(4.1,4.1,1.1,1.1), mgp=c(2.5,0.75,0), cex.axis=0.875) on.exit(par(oldpar)) attach(primates) plot(x=Bodywt, y=Brainwt, pch=16, xlab="Body weight (kg)", ylab="Brain weight (g)", xlim=c(0,300), ylim=c(0,1500)) chw <- par()$cxy[1] chh <- par()$cxy[2] text(x=Bodywt, y=Brainwt+c(-.125,0,0,.125,0)*chh, labels=row.names(primates), pos=4) detach(primates) if(device!="")dev.off() } `g1.3` <- function(device=""){ if(device!="")hardcopy(width=1.6, height=1, device=device, pointsize=7) oldpar <- par(mar=rep(0,4), mgp=rep(0,3)) on.exit(par(oldpar)) ## Start new plot; draw circle: center at (0,0), radius=0.95 symbols(0, 0, circles=0.95, bg="gray", xlim=c(-1,2.25), ylim=c(-1,1), bty="n", inches=FALSE) # inches=FALSE: radius is in x-axis units text(1.75, 0, expression("Area" == pi*phantom("'")*italic(r)^2)) ## Character strings are allowed as 'variables' in 'expressions' arrows(0, 0, -0.95, 0, length=.05, code=3) # code=3: arrows at both ends text(-0.45, -strheight("R"), expression(italic(r))) if(device!="")dev.off() } `g1.4` <- function(width=4.0, height=2.25, pointsize=c(7,5), device=""){ if(device!="") hardcopy(width=width, height=height, color=F, pointsize=pointsize, trellis=TRUE, device=device) trellis.par.set(list(superpose.symbol=list(pch=c(4,1), col=c(1,1)), superpose.line= list(lty=1:2))) ## Specify after opening any new graphics device here <- ais$sport %in% c("Row", "Swim") gph <- xyplot(ht ~ wt | sport, groups=sex, subset=here, data=ais, auto.key=list(columns=2), aspect=1, scales=list(tck=0.5)) print(trellis.par.get("fontsize")) print(gph) ## The parameter "span" controls the extent of smoothing. if(device!="")dev.off() } `gdump` <- function(fnam=NULL, prefix="~/r-book/ed2/figures/figs", splitchar="/ch"){ if(is.null(fnam)){ path <- getwd() pathtag <- strsplit(path, "/ch", fixed=TRUE)[[1]] fnam <- paste(prefix, pathtag[length(pathtag)], ".R", sep="") } else fnam <- paste(prefix, fnam, sep="/") objnames <- c(objects(pattern="^g", envir=sys.frame(0)), "hardcopy") cat("\nDump to file:", fnam, "\n") print(objnames) dump(objnames, fnam) } `gsave` <- function(fnam=NULL, prefix="~/r-book/ed2/figures/figs", splitchar="/ch", xtras=c("hardcopy","renum.fun","renum.files")){ if(is.null(fnam)){ path <- getwd() pathtag <- strsplit(path, "/ch", fixed=TRUE)[[1]] fnam <- paste(prefix, pathtag[length(pathtag)], ".RData", sep="") } else fnam <- paste(prefix, fnam, sep="/") objnames <- c(objects(pattern="^g", envir=sys.frame(0)), xtras) cat("\nDump to file:", fnam, "\n") print(objnames) save(list=objnames, file=fnam) } `hardcopy` <- function(width=3.75, height=3.75, color=FALSE, trellis=FALSE, device=c("","pdf","ps"), path=getwd(), file=NULL, format=c("nn-nn", "name"), split="\\.", pointsize=c(8,4), fonts=NULL, horiz=FALSE, ...){ if(!trellis)pointsize <- pointsize[1] funtxt <- sys.call(1) nam <- strsplit(as.character(funtxt), "(", fixed=TRUE)[[1]][1] suffix <- switch(device, ps=".eps", pdf=".pdf") if(is.character(path) & nchar(path)>1 & substring(path, nchar(path))!="/") path <- paste(path, "/", sep="") if(is.null(file)) if(format[1]=="nn-nn"){ if(!is.null(split))dotsplit <- strsplit(nam, split)[[1]] else dotsplit <- nam if(length(dotsplit)==1)dotsplit <- c("", dotsplit) nn2 <- paste(if(nchar(dotsplit[2])==1)"0" else "", dotsplit[2], sep="") if(nchar(dotsplit[1])>0){ numstart <- which(unlist(strsplit(dotsplit[1], "")) %in% paste(0:9))[1] nn1 <- substring(dotsplit[1], numstart) nn1 <- paste(if(nchar(nn1) == 1) "0" else "", nn1, "-", sep="") } else nn1 <- "" file <- paste(nn1, nn2, sep="") } else file <- nam if(nchar(file)>4 & substring(file, nchar(file)-nchar(suffix)+1)==suffix) suffix <- "" file <- paste(path, file, suffix, sep="") print(paste("Output will be directed to file:", file)) dev.out <- device[1] dev.fun <- switch(dev.out, pdf=pdf, ps=postscript) if(trellis){ library(lattice) if(device=="ps") trellis.device(file=file, device=dev.fun, color = color, horiz=horiz, fonts=fonts, width=width, height=height, ...) else trellis.device(file=file, device=dev.fun, fonts=fonts, color = color, width=width, height=height, ...) trellis.par.set(list(fontsize=list(text=pointsize[1], points=pointsize[2]))) } else if (dev.out!=""){ print(c(width, height)) if(device=="ps") dev.fun(file=file, paper="special", horiz=horiz, fonts=fonts, width=width, height=height, pointsize=pointsize[1], ...) else dev.fun(file=file, paper="special", fonts=fonts, width=width, height=height, pointsize=pointsize[1], ...) } if(trellis)trellis.par.set(list(fontsize=list(text=pointsize[1], points=pointsize[2]))) }