## Date: Wed, 24 Nov 2004 16:09:38 +0100
## From: Olivier Renaud <Olivier.Renaud@pse.unige.ch>
## Subject: [S] interaction plots within trellis
## To: Snews <s-news@wubios.wustl.edu>
## Is it easy with S+ to have side by side several interaction plots
## between two factors, given the value of a 3rd factor (e.g. with
## trellis) ? In R, a slight modification of an example of bwplot help
## file does the trick (see below), but can it be transformed for S+?
##
## Sincerely,
## Olivier Renaud
##
## Olivier.Renaud@pse.unige.ch http://www.unige.ch/~renaud/
## Methodology and Data Analysis - Faculty of Psychology and Education
## University of Geneva - 40, Bd du Pont d'Arve - CH-1211 Geneva 4
## Yes, it is just as easy in S-Plus as in R. There are slight
## differences in the specification. I give several versions of the
## display in both R and S-Plus.
## A. Slightly revised version of the Olivier Renaud example
## using bwplot in R.
## B. Regular S graphics interaction.plot function that works
## in both S-Plus and R.
## C. Standard trellis xyplot that works in both S-Plus and R.
## D. bwplot that works in S-Plus.
## A. R. This example, as written, does not work in S-Plus.
## This is the Olivier Renaud R program with several changes:
## 1. I reformatted it.
## 2. The original had a misplaced right parenthesis that
## prevented the columns argument in the key from being seen.
## 3. I defined the condition as a factor to get informative labels
## in the strip.
library(lattice)
data(OrchardSprays)
bwplot(decrease ~ treatment | factor(!rowpos<5, labels=c("1:4","5:8")),
groups = rowpos,
data=OrchardSprays,
panel = "panel.superpose",
panel.groups = "panel.linejoin",
xlab = "treatment",
key = list(
lines = Rows(trellis.par.get("superpose.line"), c(1:7, 1)),
text = list(lab = as.character(unique(OrchardSprays$rowpos))),
columns = 4,
title = "Row position"))
## Notice that even though Olivier Renaud used bwplot, he also used
## panel = "panel.superpose". Therefore there are no boxplots.
##
## The use of bwplot in R with panel.linejoin makes it unnecessary to
## sort the data by the treatment. The use of xyplot with either
## panel.groups="panel.linejoin" or type="l" does require sorting the
## data.
## For S-Plus users, I give the OrchardSprays data as the result of dput()
## R> dput(OrchardSprays)
OrchardSprays <-
structure(list(decrease = c(57, 95, 8, 69, 92, 90, 15, 2, 84,
6, 127, 36, 51, 2, 69, 71, 87, 72, 5, 39, 22, 16, 72, 4, 130,
4, 114, 9, 20, 24, 10, 51, 43, 28, 60, 5, 17, 7, 81, 71, 12,
29, 44, 77, 4, 27, 47, 76, 8, 72, 13, 57, 4, 81, 20, 61, 80,
114, 39, 14, 86, 55, 3, 19), rowpos = c(1, 2, 3, 4, 5, 6, 7,
8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4,
5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1,
2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8), colpos = c(1, 1,
1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6,
6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8),
treatment = structure(as.integer(c(4, 5, 2, 8, 7, 6, 3, 1,
3, 2, 8, 4, 5, 1, 6, 7, 6, 8, 1, 5, 4, 3, 7, 2, 8, 1, 5,
3, 6, 7, 2, 4, 5, 4, 7, 1, 3, 2, 8, 6, 1, 3, 6, 7, 2, 4,
5, 8, 2, 7, 3, 6, 1, 8, 4, 5, 7, 6, 4, 2, 8, 5, 1, 3)), .Label = c("A",
"B", "C", "D", "E", "F", "G", "H"), class = "factor")), .Names = c("decrease",
"rowpos", "colpos", "treatment"), row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48",
"49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
"60", "61", "62", "63", "64"), class = "data.frame")
## B. Regular graphics, using standard S functions, with the same
## placement of the legends in both panels. This example works in
## both S-Plus and R.
attach(OrchardSprays)
interaction.plot(treatment, rowpos, decrease, col=c(2:9))
tmp.ylim <- par()$usr[3:4]
detach("OrchardSprays")
par(mfrow=c(1,2))
attach(OrchardSprays[ OrchardSprays$rowpos<5,])
interaction.plot(treatment, rowpos, decrease,
ylim=tmp.ylim, col=c(2:5))
title(main="rowpos=1:4")
detach(2)
attach(OrchardSprays[!OrchardSprays$rowpos<5,])
interaction.plot(treatment, rowpos, decrease,
ylim=tmp.ylim, col=c(6:9))
title(main=paste("rowpos=5:8"))
detach(2)
par(mfrow=c(1,1))
## C. Standard trellis with data reordered in the call. The data must be
## ordered by the x-factor (treatment in this example). This example
## works in both S-Plus and R. The "as.numeric()" and the "scales="
## are required with xyplot in S-Plus and optional in R.
xyplot(decrease ~ as.numeric(treatment) |
factor(!rowpos<5, labels=c("1:4","5:8")),
OrchardSprays[order(OrchardSprays$treatment), ],
groups = OrchardSprays$rowpos[order(OrchardSprays$treatment)],
panel = "panel.superpose", type="l",
xlab = "treatment",
scales=list(x=list(
at=1:length(levels(OrchardSprays$treatment)),
labels=levels(OrchardSprays$treatment), alternating=F)
),
key = list(
lines = Rows(trellis.par.get("superpose.line"), c(1:7, 1)),
text = list(
lab = as.character(unique(OrchardSprays$rowpos))), ## original order
columns = 4,
title = "Row position")
)
## D. S-Plus only. The use of bwplot instead of xyplot means the
## "as.numeric()" and the "scales=" are not needed. The data must be
## sorted by treatment. The S-Plus bwplot insists that the response
## variable be on the right. To put the response variable on the
## vertical axis, we use the HH t.trellis function to transpose each
## panel of the trellis object. If you run this example without the
## HH library, skip the transpose and you will get a plot with the
## response variable on the horizontal axis.
t( ## t.trellis from HH
bwplot(treatment ~ decrease | factor(!rowpos<5, labels=c("1:4","5:8")),
groups = rowpos,
data=OrchardSprays[order(OrchardSprays$treatment), ],
panel = "panel.superpose",
type="l",
ylab = "treatment",
key = list(
lines = Rows(trellis.par.get("superpose.line"), c(1:7, 1)),
text = list(lab = as.character(unique(OrchardSprays$rowpos))),
columns = 4,
title = "Row position"))
)
## For information on the HH library, please go to the website for our
## book
## Statistical Analysis and Data Display
## Richard M. Heiberger and Burt Holland
## at
## http://springeronline.com/0-387-40270-5
|