`g14.1` <- function(device="",new=F, ycol = -2.1-(0:9)*2.1){ if(device!="")hardcopy(width=5.4, height=5.0, device=device) else if(new)x11(width=6.0, height=9) ftype <- c("plain","bold","italic","bold italic","symbol") yline <- 4.2 ypmax <- 20 farleft <- -6 oldpar <- par(mar=c(0,0,0.5,0)) on.exit(par(oldpar)) plot(c(-8,31), c(-1, ypmax), type="n", xlab="", ylab="", axes=F) chh <- par()$cxy[2] text(0:25, rep(ypmax+0.8*chh,26), paste(0:25), srt=90, cex=0.75, xpd=T) text(-1.5, ypmax+0.8*chh, "pch =", cex=0.75, xpd=T) text(farleft, ypmax-0.5, "font", xpd=T) points(0:25, rep(ypmax,26), pch=0:25) letterfont <- function(ypos=ypmax, font=2){ par(font=font) text(-1.35, ypos, "64-76", cex=0.75, adj=1, xpd=TRUE) text(19-1.35, ypos, "96-108", cex=0.75, adj=1) points(c(0:12), rep(ypos,13), pch=64:76) points(19:31, rep(ypos,13), pch=96:108) text(farleft, ypos, paste(font), xpd=T) text(farleft, ypos-0.5, ftype[font], cex=0.75) } plotfont <- function(xpos=0:31, ypos=ypmax, font=1, sel32=2:8, showfont=TRUE){ par(font=font) i <- 0 for (j in sel32){ i <- i+1 text(-1.35, ypos-i+1, paste((j-1)*32,"-", j*32-1, sep=""), cex=0.75, adj=1, xpd=TRUE) points(xpos, rep(ypos-i+1,32), pch=(j-1)*32+(0:31)) } lines(rep(-1.05,2),c(ypos-length(sel32)+1, ypos)+c(-.4, .4), xpd=T, col="grey40") if(showfont){ text(farleft, ypos, paste(font), xpd=T) text(farleft, ypos-0.5, ftype[font], cex=0.75, xpd=T) } } plotfont(ypos=ypmax-1.5, font=1, sel32=2:8) for(j in 2:4)letterfont(ypos=ypmax-6-1.4*j, font=j) plotfont(ypos=ypmax-13.1, font=5, sel32=3) plotfont(xpos=c(-0.5,1:31), ypos=ypmax-14.1, font=5, sel32=4, showfont=FALSE) par(font=1) text(farleft, yline+0.9, "lty", xpd=TRUE) ltypes <- c("blank","solid","dashed","dotted","dotdash", "longdash","twodash") lcode <- c("","","44","13","1343","73","2262") for(i in 0:6){lines(c(3,31), c(yline-0.8*i,yline-0.8*i), lty=i, lwd=2, xpd=T) text(farleft, yline-0.8*i, paste(i), xpd=TRUE) text(farleft+2.5, yline-0.8*i, ltypes[i+1], cex=0.85, xpd=TRUE) text(farleft+6, yline-0.8*i, lcode[i+1], cex=0.85, xpd=TRUE) } if(device!="")dev.off() } `g14.2` <- function(device=""){ if (device != "") hardcopy(width = 2.25, height = 2.5, pointsize=c(7,4), device = device) annotate <- function(b){ ## Fit least squares regression line: log(wood) vs log(dbh) b <- coef(lm(log(wood) ~ log(dbh), data=Acmena[-largest2[1], ])) a <- round(exp(b[1]),3); b2 <- round(b[2],3) arg1 <- bquote(italic(y) == .(A) * phantom(0) * italic(x)^.(B), list(A=a, B=b2)) arg2 <- quote("where " * italic(y) * " " * "= wood; " * " " * italic(x) * " " * "= dbh") legend("topleft", legend=do.call("expression", list(arg1, arg2)), bty="n", cex=0.8) } oldpar <- par(pty="s") on.exit(par(oldpar)) Acmena <- subset(rainforest, species=="Acmena smithii") plot(wood~dbh, data=Acmena, xlim=1.025*c(0, max(Acmena$dbh))) largest <- max(Acmena$dbh) Acmena.lm <- lm(log(wood) ~ log(dbh), data=Acmena, subset=dbh < max(dbh)) b <- coef(Acmena.lm) curve(exp(b[1])*x^b[2], to=largest2[2], add=TRUE) curve(exp(b[1])*x^b[2], from=largest2[2], to=largest2[1], lty=2, add=TRUE) annotate(b=round(b,3)) mtext(side=3, line=1.5, substitute(italic(tx) * ": " * "wood vs dbh", list(tx="Acmena smithii")), cex=1.1) 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("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="~/r-book/ed2/Art/", 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]))) }