Hi, I am trying to plot an interaction.plot with different color for each level of a factor. It has an erratic behavior. For example, it works for the first interaction.plot below, with the example from the ALDA book, but not with the other plots, from the NPK dataset: # from http://www.ats.ucla.edu/stat/r/examples/alda/ch2.htm tolerance <- read.csv("http://www.ats.ucla.edu/stat/r/examples/alda/data/tolerance1.txt") tolerance.pp <- read.csv("http://www.ats.ucla.edu/stat/r/examples/alda/data/tolerance1_pp.txt") attach(tolerance.pp) # fitting the linear model by id fit <- by(tolerance.pp, id, function(bydata) fitted.values(lm(tolerance ~ time, data=bydata))) fit <- unlist(fit) # plotting the linear fit by id interaction.plot(age, id, fit, xlab="age", ylab="tolerance") # plots red for female and green for male interaction.plot(age, id, fit, xlab="age", ylab="tolerance", col=male+2) print(tolerance.pp[id==978|id==514|id==9|id==1105|id==1653,c("id","male")]) data(npk, package="MASS") fit <- by(npk, npk$block, function(bydata) fitted.values(lm(yield ~ N, data=bydata))) fit <- unlist(fit) interaction.plot(npk$N, npk$block, fit, xlab="N", ylab="yield") # fake factor, numeric fac <- c(rep(1,12),rep(2,12)) # plots everything in black interaction.plot(npk$N, npk$block, fit, xlab="N", ylab="yield",col=fac) cbind(npk$block,fac) # plots everything transparent fac <- as.factor(fac) interaction.plot(npk$N, npk$block, fit, xlab="N", ylab="yield",col=as.numeric(npk$fac))
I was unable to run your code; 'fac' is missing and npk$fac in teh interaction.plot returns NA.> data(npk, package="MASS") > fit <- by(npk, npk$block, function(bydata) fitted.values(lm(yield ~ N, > data=bydata))) > fit <- unlist(fit) > interaction.plot(npk$N, npk$block, fit, xlab="N", > ylab="yield") # fake factor, numeric fac <- > c(rep(1,12),rep(2,12)) # plots everything in black > interaction.plot(npk$N, npk$block, fit, xlab="N", > ylab="yield",col=fac) > cbind(npk$block,fac) > # plots everything transparent > fac <- as.factor(fac) > interaction.plot(npk$N, npk$block, fit, xlab="N", > ylab="yield",col=as.numeric(npk$fac))******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Iuri: Your code as emailed reads: ###### data(npk, package="MASS") fit <- by(npk, npk$block, function(bydata) fitted.values(lm(yield ~ N, data=bydata))) fit <- unlist(fit) interaction.plot(npk$N, npk$block, fit, xlab="N", ylab="yield") # fake factor, numeric fac <- c(rep(1,12),rep(2,12)) # plots everything in black interaction.plot(npk$N, npk$block, fit, xlab="N", ylab="yield",col=fac) cbind(npk$block,fac) # plots everything black as well fac <- as.factor(fac) interaction.plot(npk$N, npk$block, fit, xlab="N", ylab="yield",col=as.numeric(fac)) ##### Close inspection shows that the definition of fac is buried in a comment on the fifth line of the code, so it was not running. Having disentangled that, did you inspect fac? fac is a length 24 vector with the first 12 values equal to 1 - corresponding to black. You are plotting only six lines .... all of which plot correctly as black when I run the code because the first 6 values in the colour vector you supply are all black. If you want to plot 6 different colour lines, try, for example, col=1:6. Hope that helps you clear things up! S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
>But I don't want to plot random colors. >... > That's why I have this vector with length 24 - each one matches one line in the "npk" dataset.... which is not what interaction.plot, or matplot, needs; it needs one per line on the plot.>How can I inform to the interaction.plot function the color >corresponding to the block it will plot?You need a colour vector the same length as the number of levels you're plotting. One easy way to do that would be to do something like col=1:nlevels(fac), for example as in interaction.plot(npk$N, npk$block, fit, xlab="N", ylab="yield",col=1:nlevels(fac)) If you have several levels that correspond to the same level of a third factor, you need to provide a cross-reference of sorts. In your toy example, fac corresponds to three levels of block, so one could specify manually. One way of doing it in code, though, could be to use table to identify levels of fac corresponding to levels of block. That sounds a bit complicated, but let's see: npk$fac <- fac #just so it's in the same data frame fac.by.block <- with(npk, table(fac, block)) #cross-reference fac levels by block fac.index.by.block.level <- apply(fac.by.block, 2, function(x) which(x>0)[1]) #Assumes that you want the first nonzero table entry and that numerical indices are OK # you could also use which directly: which(fac.by.block >1, arr.ind=TRUE)[,1] gives the same result IF there's a 1:1 fac:block matching Then interaction.plot(npk$N, npk$block, fit, xlab="N", ylab="yield",col=(1:nlevels(fac))[fac.index.by.block.level]) ... which I think is something like what you;re after? S ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}