book
Article ID: KB0080993
calendar_today
Updated On:
Description
Some information and some examples of how to use subscripts and groups in your trellis graphs:
Subscripts
The subscripts argument in trellis plots represent the rows in your original data set that print in each panel.
For example:
xyplot(Weight ~ Disp. | Type, data=fuel.frame, panel=function(x,y,subscripts,...) {
print(subscripts)
panel.xyplot(x,y,...)
})
produces:
[1] 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #panel 1
[1] 51 52 53 #panel 2
[1] 38 39 40 41 42 43 44 45 46 47 48 49 50 #panel 3
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 #panel 4
[1] 14 15 16 17 18 19 20 21 22 #panel 5
[1] 54 55 56 57 58 59 60 #panel 6
You can use subscripts to add other data to an existing plot. For example:
fuel.frame$Disp2 <- fuel.frame$Disp. + 25
xyplot(Weight ~ Disp. | Type, data=fuel.frame, panel=function(x,y,subscripts,...) {
panel.xyplot(x,y,...)
panel.xyplot(fuel.frame$Disp2[subscripts],fuel.frame$Weight[subscripts],col=5, pch=5)
})
Notice that in the original xyplot() call you have y ~ x, but in panel.xyplot() the arguments are listed as (x,y).
Now let's say that you wanted to add data from a different source, you have to pay attention if they
have different number of rows. Let's try this:
newdata <- data.frame(newWeight=sample(fuel.frame$Weight, 25, replace=T), newDisp=sample(fuel.frame$Disp., 25, replace=T),
newType=sample(fuel.frame$Type, 25))
xyplot(Weight ~ Disp. | Type, data=fuel.frame, panel=function(x,y,subscripts,...) {
panel.xyplot(x,y,...)
panel.xyplot(newdata$newDisp[subscripts],newdata$newWeight[subscripts],col=5, type="l")
})
The newdata information only prints in half of the panels, because there are only 25 lines in new data but
subscripts goes to 60. In this case we need to create a new way to subscript the newdata so that it
prints in each panel. Here's one way to do that:
xyplot(Weight ~ Disp. | Type, data=fuel.frame, panel=function(x,y,subscripts,...) {
my.grp <- unique(fuel.frame$Type[subscripts])
panel.xyplot(x,y,...)
panel.xyplot(newdata$newDisp[newdata$newType==my.grp],newdata$newWeight[newdata$newType==my.grp], col=5, type="l")
})
####################
Groups
The groups argument in trellis plots allow you to create multiple groupings within each panel.
This can be handled in a couple of ways, either as an argument in the main plot call or in a panel functions.
Here are two examples of using it as an argument in the original plot call:
1.
my.df <- data.frame(Time=rep(rep(1:5,each=6),3), Y.num=rnorm(90,10),
X=factor(rep(rep(1:2,each=3),15)), Y=rep(letters[1:3],30), Z=rep(c("P","Q","R"),each=30))
xyplot(Y.num~Time|X*Y, data=my.df, groups=Z, type="l",
panel=function(x,y,...) panel.superpose(x, y, col=c(2,3,6),...),
key=list(lines=list(col=c(2,3,6)), text=levels(my.df$Z), columns=3))
2.
my.df <- data.frame(Height=rnorm(432,70,5), Weight=rnorm(432,170,10),
Age=rep(2:7,72), Ethnicity=rep(c("A","B"),each=216),
Sex=rep(rep(c("F1","F2","F3","M1","M2","M3"),each=12),6))
xyplot(Height~Weight | Age*Ethnicity, data=my.df, groups=Sex,
panel=panel.superpose, type="o")
Here is are two examples of using it passed into a panel function:
fuel.frame <- fuel.frame
temp <- ltsreg(Fuel~Mileage, data=fuel.frame)
fuel.frame[,"Fit"] <- as.vector(temp$fitted.values)
temp2 <- tapply(fuel.frame$Fit, fuel.frame$Type, print)
1.
mypanel <- function(x, y, subscripts, groups) {
panel.xyplot(x, y)
this.level <- groups[subscripts][1]
print(this.level)
panel.xyplot(x, y=as.vector(unlist(temp2[this.level])), type="l")
}
xyplot(Fuel ~ Mileage | Type, data=fuel.frame,
groups=Type, panel=mypanel)
2.
mypanel <- function(x, y, subscripts, groups) {
panel.xyplot(x, y)
this.level <- groups[subscripts][1]
panel.lmline(x, y=as.vector(unlist(temp2[this.level])), lty=3)
}
xyplot(Fuel ~ Mileage | Type, data=fuel.frame,
groups=Type, panel=mypanel)
Issue/Introduction
Discussion of subscripts and groups in trellis graphs
Environment
Product: TIBCO Spotfire S+
Version: All supported versions
OS: All supported operating systems
--------------------