[R] Colors in interaction plots

S Ellison S.Ellison at LGCGroup.com
Thu Jan 17 17:59:45 CET 2013


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}}



More information about the R-help mailing list