Unformatted text preview:

Trellis graphics 1 Trellis graphics Extremely useful approach for graphical exploratory data analysis (EDA) Allows to examine for complicated, multiple variable relationships. Types of plots xyplot: scatterplot bwplot: boxplots stripplot: display univariate data against a numerical variable dotplot: similar to stripplot histogram densityplot: kernel density estimates barchart piechart: (Not available in R) splom: scatterplot matrices contourplot: contour plot of a surface on a regular grid levelplot: pseudo-colour plot of a surface on a rectangular gridTrellis graphics 2 wireframe: perspective plot of a surface evaluated on a regular grid cloud: perspective plot of a cloud of points (3D scatterplot) Note in S-Plus, the trellis graphics functions are contained in the library, trellis. This library should be loaded automatically when starting S-Plus. However if it isn’t, give the command library(trellis). In R, the trellis graphics features are usually not automatically loaded. To load them you need to give the command library(lattice).Trellis graphics 3 <digression> .First() and .Last() functions It is possible to configure S-Plus and R to load certain files, override defaults, etc when they start up with the use of the .First() function. This is similar to the use of .cshrc/.bashrc and .login files in Unix. Any commands that you wish to run everytime you start up S-Plus and R should go in the .First() function. An example of a .First() function is .First<- function() { library(lattice) options(contrasts=c(“contr.treatment”, “contr.poly”)) } So everytime that S-Plus/R is run from the directory containing this .First() function, these two commands will be run. The options() command sets the default contrasts when dealing with factors in linear models. The .Last() function is similar to Unix’s .logout file in that it contains the commands you want run when quitting S-Plus/R. </digression>Trellis graphics 4 Trellis graph disttime 50100150200 5 10 15 20 25 Usual approach 5 10 15 20 2550 100 150 200disttimeTrellis graphics 5 Trellis approach xyplot(time ~ dist, data=hills, panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.lmline(x, y ,type="l") panel.abline(ltsreg(x, y), lty=3) } ) Usual plotting approach attach(hills) plot(dist, time) abline(lm(time ~ dist)) abline(ltsreg(time ~ dist), lty=3) detach()Trellis graphics 6 Trellis approach Speed of Light DataSpeedExperiment No.12345 600 700 800 900 1000 1100 Usual approach 1 2 3 4 5700 800 900 1000Speed of Light DataExperiment No.Trellis graphics 7 Trellis approach bwplot(Expt ~ Speed, main="Speed of Light Data", ylab="Experiment No.", data=morley, panel = function(x, y, ...) { panel.bwplot(x, y, ...) panel.abline(v=734.5) } ) Usual plotting approach attach(morley) plot.factor(Expt, Speed, main="Speed of Light Data", xlab="Experiment No.") abline(h=734.5) detach(morley) Instead of plot.factor, the boxplot command boxplot(split(Speed,Expt), main="Speed of Light Data", xlab="Experiment No.") could be used as well.Trellis graphics 8 Many trellis type plots can not be done using the more standard graphics commands, or are very complicated. One example is a scatter plot matrix Scatter Plot MatrixFertility404050506060707080809090Agriculture0020204040404060608080Examination1010202020203030Education0010102020303040405050Catholic002020404060608080100100Infant.Mortality1010151520202525 splom(~ swiss.df, aspect="fill", panel = function(x, y, ...) { panel.xyplot(x, y, ...); panel.loess(x, y, ...) } )Trellis graphics 9 This example shows the power of panel functions, as the basic plot commands can easily be applied to multi-panel displays. In this example the scatterplot smoother loess is applied to each panel of the plot ViscosityTime 0 50100150200250 0 50 100 150 200 250 300Weight: 20 grams 50 grams 100 grams sps<-trellis.par.get("superpose.symbol") sps$pch <- 1:7 trellis.par.set("superpose.symbol",sps) xyplot(Time ~ Viscosity, data=stormer, groups = Wt, panel = panel.superpose, type="b", key = list(columns=3, text= list(paste(c("Weight: ","",""), unique(stormer$Wt), "grams")), points=Rows(sps,1:3)))Trellis graphics 10 There are an number approaches with Trellis graphics for displaying 3 dimensional information 0 1 2 3 4 5 61 2 3 4 5 6-700-750-800-850-900-950 topo.plt <- expand.grid(topo.mar) topo.plt$pred <- as.vector(predict(topo.loess,topo.plt)) levelplot(pred ~ x*y, topo.plt,aspect=1, at = seq(690, 960, 10), xlab="", ylab="", panel = function(x,y,subscripts, ...) { panel.levelplot(x,y,subscripts, ...) panel.xyplot(topo$x, topo$y, cex=0.5, col=1) } )Trellis graphics 11 xpredpred700750800850900950 wireframe(pred ~ x*y, topo.plt,aspect=c(1,0.5), drape=T, screen=list(z=-150, x=-60), colorkey=list(space="right", height=0.6))Trellis graphics 12 01234560123456 700 725 750 775 800 800 825 850 850 875 875 875 875 900 900 925 925 950 contourplot(z ~ x * y, mat2tr(topo.lo), aspect=1, at = seq(700, 1000, 25), xlab="", ylab="", panel = function(x,y,subscripts, ...) { panel.contourplot(x,y,subscripts, ...) panel.xyplot(topo$x, topo$y, cex=0.5) } ) Note that these last two plots are from S-Plus not R (where the rest of today’s plots are from. I couldn’t get the code to work properly in R for these two plots.Trellis graphics 13 Where Trellis plots get particularly useful is when we condition. Scatter Plot MatrixComp.1-1-1-0.5-0.5000.50.5111.51.5Comp.2-0.1-0.1-0.05-0.05000.050.050.10.10.150.15Comp.3-0.1-0.1-0.05-0.0500000.050.050.10.1Blue male Blue female Orange male Orange female lcrabs <- log(crabs[,4:8]) lcrabs.pc <- predict(princomp(lcrabs)) crabs.grp <- c("B","b","O","o")[rep(1:4,rep(50,4))] splom(~lcrabs.pc[,1:3], groups=crabs.grp, panel = panel.superpose, key = list(text=list(c("Blue male","Blue female", "Orange male", "Orange female")), points=Rows(trellis.par.get("superpose.symbol"),1:4), columns=4))Trellis graphics 14 BlueFemaleComp.1Comp.2Comp.3OrangeFemaleComp.1Comp.2Comp.3BlueMaleComp.1Comp.2Comp.3OrangeMaleComp.1Comp.2Comp.3 sex <- crabs$sex; levels(sex) <- c("Female","Male") sp <- crabs$sp; levels(sp) <- c("Blue","Orange") splom(~lcrabs.pc[,1:3] | sp*sex, pscales=0)Trellis graphics 15 The princomp command calculates the principle components of the data. Principle components finds linear combinations of the data, such that each is orthogonal to each other, and they


View Full Document

HARVARD STAT 335 - Trellis graphics

Documents in this Course
Load more
Download Trellis graphics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Trellis graphics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Trellis graphics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?