[R] plotting results from tapply

Michael Rennie mrennie at utm.utoronto.ca
Fri Jan 26 17:50:57 CET 2007


Hi, there

I'm trying to plot what is returned from a call to tapply, and can't figure 
out how to do it. My guess is that it has something to do with the 
inclusion of row names when you ask for the values you're interested in, 
but if anyone has any ideas on how to get it to work, that would be 
stellar.  Here's some example code:

y1<-rnorm(40, 2)
x1<-rep(1:2, each=20)
x2<-rep(1:2, each=10 times=2)

ex.dat<-data.frame(cbind(y1,x1,x2))

ex.dat$x1<-factor(ex.dat$x1, labels=c("A", "B"))
ex.dat$x2<-factor(ex.dat$x2, labels=c("C", "D"))

attach(ex.dat)

xbar<-tapply(ex.dat$y1, ex.dat[,-1], mean)
xbar

#values I'd like to plot:
row.names(xbar) #levels of x1
xbar[,1] #mean response of y1 for group C (x2) across x1

#plot mean response y1 for group C against x1 (i.e., using x2 as a grouping 
factor).
plot(row.names(xbar), xbar[,1], ylim=range[y1])

#This is where things break down. The error message states that I need 
"finite xlim values" but I haven't assigned anything to xlim. If I just 
plot the data:

plot(x1, y1)

#This works fine.

#And, I can get this to work:

stripchart(xbar[1,]~row.names(xbar), vert=T)

#However, I'd like to then add the values for the second set of means 
(i.e., mean values for group D against x1, or (xbar[,2])) to the plot.
#I tried following up the previous command with:

points(row.names(xbar), xbar[,2])

#But that returns an error as well (NAs introduced by coercion).



Any suggestions?

Cheers,

Mike

PS- some of you might suggest for me to use interaction.plot, since this is 
essentially what I'm building here. True, but I can't get error bars using 
interaction.plot. I'm therefore trying to build my own version where I can 
specify the inclusion of error bars. Presumably the interaction.plot has 
figured out how to do what I'm attempting, so I have some faith that I am 
on the right track....





Michael Rennie
Ph.D. Candidate, University of Toronto at Mississauga
3359 Mississauga Rd. N.
Mississauga, ON  L5L 1C6
Ph: 905-828-5452  Fax: 905-828-3792
www.utm.utoronto.ca/~w3rennie



More information about the R-help mailing list