## Chapter 1: Preliminaries ## Sec 1.1: Installation of R and of R Packages ## ss 1.1.1: Installation of packages from the command line install.packages("Rcmdr", dependencies=TRUE) install("ggplot2", dependencies=TRUE) ## Sec 1.2: The R Commander library(Rcmdr) ## Chapter 2: Base Graphics demo(graphics) ## Sec 2.1: {plot()} and allied functions library(DAAG) attach(elasticband) # R can now find distance & stretch plot(distance ~ stretch) plot(ACT ~ year, data=austpop, type="l") plot(ACT ~ year, data=austpop, type="b") detach(elasticband) ## Invoke once device is open, and before starting the plot oldpar <- par(mar = rep(2,4), xaxs="i", yaxs="i", mgp=c(1.5,0.75,0)) ## Sec 2.2: Plotting Mathematical Symbols par(family="Times") plot(hg ~ rcc, data=ais, xlab=expression("Red cell count (" * 10^12 * italic(l)^{-1} * ")" ), ylab=expression("Hemaglobin (" * g*dot(" ") * daL^{-1} * ")" )) ## Code used for plot: r <- seq(0.1, 8.0, by=0.1) plot(r, pi * r^2, xlab=expression(Radius == r), ylab=expression(Area == pi*r^2), type="l") # NB: Use ==, within an expression, to print = ## Sec 2.3: Summary ## Sec 2.4: Exercises huron <- data.frame(year=as(time(LakeHuron), "vector"), mean.height=LakeHuron) identify(huron$year, huron$mean.height, labels=huron$year) lag.plot(huron$mean.height) plot(LakeHuron) identify(LakeHuron, labels=time(LakeHuron)) ## Chapter 3: Lattice Graphics library(lattice) demo(lattice) ## Sec 3.1: Lattice Graphics vs Base Graphics library(lattice) plot(ACT ~ year, data=austpop) # Base graphics xyplot(ACT ~ year, data=austpop) # Lattice graphics invisible(plot(ACT ~ year, data=austpop)) # A graph is plotted invisible(xyplot(ACT ~ year, data=austpop)) # Graph does appear print(xyplot(ACT ~ year, data=austpop)) ## Sec 3.2: Groups within Data, and/or Columns in Parallel xyplot(Beer+Spirit+Wine ~ Year | Country, data=grog, outer=FALSE, xlab="", ylab="Amount consumed (per person)", ylim=c(0,5.5), auto.key=list(columns=3)) xyplot(Beer ~ Year | Country, data=grog) # Plot for Beer xyplot(Spirit ~ Year | Country, data=grog) # Plot for Spirit xyplot(Wine ~ Year | Country, data=grog) # Plot for Wine xyplot(Beer+Spirit+Wine ~ Year, groups=Country, data=grog, auto.key=list(columns=2) ) ## Sec 3.3: Lattice Parameters and Graphics Features ## ss 3.3.1: Point, line and fill color settings miscSettings <- simpleTheme(pch = c(1,3,4), cex=1.25) xyplot(Beer+Spirit+Wine ~ Year | Country, outer=FALSE, auto.key=list(columns=3), data=grog, par.settings=miscSettings) trellis.par.set(miscSettings) xyplot(Beer+Spirit+Wine ~ Year | Country, outer=FALSE, auto.key=list(columns=3), data=grog) basicplot <- xyplot(Beer ~ Year | Country, data=grog) update(basicplot, par.settings=miscSettings) miscSettings <- simpleTheme(pch = c(1,3,4), cex=1.25) xyplot(Beer+Spirit+Wine ~ Year | Country, outer=FALSE, auto.key=list(columns=3), data=grog, ylim=c(0,5.5), par.settings=miscSettings, xlab="", ylab="Alcohol consumption (per person)") trellis.par.set(simpleTheme(pch = 16, cex=2)) names(trellis.par.get()) trellis.device(color=FALSE) show.settings() trellis.device(color=TRUE) show.settings() trellis.par.set(list(fontsize = list(text = 7, points = 4))) ## ss 3.3.2: Parameters that affect axes, tick marks, and axis labels jobplot <- xyplot(Ontario+BC ~ Date, data=jobs) ## Half-length ticks, each quarter, Label years, Add key tpos <- seq(from=95, by=0.25, to=97) tlabs <- rep(c("Jan95", "", "Jan96", "", "Jan97"), c(1,3,1,3,1)) update(jobplot, auto.key=list(columns=2), xlab="", scales=list(tck=0.5, x=list(at=tpos, labels=tlabs))) logplot <- xyplot(Ontario+BC ~ Date, data=jobs, outer=TRUE, xlab="", scales=list(y=list(log="e"))) update(logplot, scales=list(y=list(relation="sliced"))) xyplot(Ontario+Quebec+BC+Alberta+Prairies+Atlantic ~ Date, data=jobs, ylab="Number of jobs", type="b", outer=FALSE, auto.key=list(space="right", lines=TRUE)) ## Save the graphics object, for later updating jobs.xyplot <- xyplot(Ontario+Quebec+BC+Alberta+Prairies+Atlantic ~ Date, data=jobs, type="b", layout=c(3,2), ylab="Number of jobs", scales=list(y=list(relation="sliced", log=TRUE)), outer=TRUE) ylabpos <- exp(pretty(log(unlist(jobs[,-7])), 100)) ylabels <- paste(round(ylabpos),"\n(", log(ylabpos), ")", sep="") ## Create a date object 'startofmonth'; use this instead of 'Date' atdates <- seq(from=95, by=0.5, length=5) datelabs <- format(seq(from=as.Date("1Jan1995", format="%d%b%Y"), by="6 month", length=5), "%b%y") update(jobs.xyplot, xlab="", between=list(x=0.5, y=1), scales=list(x=list(at=atdates, labels=datelabs), y=list(at=ylabpos, labels=ylabels), tck=0.6) ) ## ss 3.3.3: A further example aisBS <- subset(ais, sport %in% c("B_Ball", "Swim")) basic.xyplot <- xyplot(hg ~ rcc | sex, groups=sport[drop=TRUE], data=aisBS) ## Simplified Code update(basic.xyplot, type=c("p","r"), auto.key=list(lines=TRUE, columns=2)) # For a smooth curve, specify # type=c("p","smooth") with(ais, table(sex,sport)) basic1 <- xyplot(hc ~ rcc | sex, groups=sport[drop=TRUE], data=subset(ais, sport %in% c("B_Ball", "Swim")), xlab="", ylab="Blood cell to plasma ratio (%)") basic2 <- update(basic1, par.settings=simpleTheme(pch = c(1,3), scales=list(tck=0.5), lty=1:2, lwd=1.5)) update(basic2, type=c("p", "r"), auto.key=list(columns=2, lines=TRUE), xlab=expression("Red cell count (10"^{12}*"."*L^{-1}*")")) ## ss 3.3.4: Keys -- \texttt{auto.key}, \texttt{key} \& \texttt{legend} ## Sec 3.4: Panel Functions and Interaction with Plots ## ss 3.4.1: Panel functions xyplot(ACT ~ year, data=austpop) xyplot(ACT ~ year, data=austpop, panel=panel.xyplot) ## ss 3.4.2: Interaction with Lattice Plots trellis.focus(highlight=FALSE) xyplot(log(Time) ~ log(Distance), groups=roadORtrack, data=worldRecords) trellis.focus() ## Now click (maybe twice) on a panel panel.identify(labels=worldRecords$Place) ## Click near to points that should be labeled ## Right click to terminate trellis.unfocus() library(DAAG) library(grid) ## Positioning will be (xmin=0, ymin=0.46, xmax=1, ymax=1) print(update(cuckoostrip, xlab=""), position=c(0, .46, 1 ,1)) trellis.focus("toplevel", highlight=FALSE) grid.text("A", x=0.05, y=0.935, gp=gpar(cex=1.15)) trellis.unfocus() print(cuckoobox, position=c(0, 0, 1, 0.54), newpage=FALSE) trellis.focus("toplevel", highlight=FALSE) grid.text("B", x=0.05, y=0.935, gp=gpar(cex=1.15)) trellis.unfocus() ## Sec 3.5: Displays of Distributions ## ss 3.5.1: Stripplots, dotplots and boxplots ## Footnote Code ## For slightly improved labeling, precede with: levels(cuckoos$species) <- sub(".", " ", levels(cuckoos$species), fixed=T) cuckoostrip <- stripplot(species ~ length, aspect=0.5, xlab="Cuckoo egg length (mm)", data=cuckoos) cuckoobox <- bwplot(species ~ length, aspect=0.5, data=cuckoos, xlab="Cuckoo egg length (mm)") dotplot(variety ~ yield | site, data = barley, groups = year, xlab = "Barley Yield (bushels/acre) ", ylab = NULL, layout = c(1, 6), aspect = 0.5, auto.key=list(labels=levels(barley$year), space = "right")) deathrate <- c(40.7, 36,27,30.5,27.6,83.5) hosp <- c("Cliniques of Vienna (1834-63)\n(> 2000 cases pa)", "Enfans Trouves at Petersburg\n(1845-59, 1000-2000 cases pa)", "Pesth (500-1000 cases pa)", "Edinburgh (200-500 cases pa)", "Frankfort (100-200 cases pa)", "Lund (< 100 cases pa)") hosp <- factor(hosp, levels=hosp[order(deathrate)]) dotplot(hosp ~ deathrate, xlim=c(0,110), cex=1.5, scale=list(cex=1.25), type=c("h","p"), xlab=list("Death rate per 1000 ", cex=1.5), sub="From Nightingale (1871) - data from Dr Le Fort") ## ss 3.5.2: Lattice Style Density Plots densityplot(~ earconch | sex, groups=Pop, data=possum, par.settings=simpleTheme(col=c("gray","black"), auto.key=list(columns=2)) ## Sec 3.6: Lattice graphics functions -- Further Points ## ss 3.6.1: Help on lattice functions ## ss 3.6.2: A list of Lattice Functions ## ss 3.6.3: Summary ## Chapter 4: The {ggplot2} Package ## Sec 4.1: Examples ## Australian rain data library(DAAG) library(ggplot2) ## A: Default loess smooth qplot(Year, seRain, data=bomsoi, geom=c("point","smooth")) ## B: 20%, 50% & 80% quantiles ## 5 d.f. normal splines qplot(Year, seRain, data=bomsoi, geom=c("point", "quantile"), formula = y ~ ns(x,5), quantiles=c(0.2,0.5,0.8) ) ## C: Robust fit using rlm() ## 15 d.f. normal splines qplot(Year, seRain, data=bomsoi, geom=c("point", "smooth"), formula = y ~ ns(x,15), method="rlm") ## Physical measurements of Australian athletes ## A qplot(wt, ht, data=ais, geom=c("point", "density2d"), facets = sex~.) ## B qplot(sport, ht, data=ais, geom="boxplot", facets = sex~.) ## Chapter 5: References and Bibliography ## Sec 5.1: Books and Papers on R ## Sec 5.2: Web-Based Information ## Sec 5.3: Graphics ## Large and Possibly Sparse Data ## Literature on trellis (lattice) graphics ## The grammar of graphics in R ( {ggplot2})