[R] Interaction Plots in R
Red Hat Linux User
pflugshaupt at cirsium.ethz.ch
Tue May 9 15:02:47 CEST 2000
Ko-Kang Wang wrote:
> Is there a function to do interaction plots in R?
> I know in Splus I can simply use interaction.plot()
>
> Thanks.
I don't think there's such a function, so I wrote one some time ago. It takes two
factors f1, f2 and a response variable data. I wanted to add error bars but dropped
that due to lack of time. If you're interested in that, I can send you what I prepared
(mainly a function to plot error bars).
Here's the code; use it at your own risk :-)
Kaspar
BTW, due to the infamous "xaxt" bug in par, the axis will not be labelled correctly in
R 1.0.0. Version 1.0.0.1 corrects it, just upgrade if you still use 1.0.0.
int.plot <- function (f1, f2, data, main = "Interaction Plot", xlab =
deparse(substitute(f2)),
ylab = deparse(substitute(f1)), ...)
{
cellmeans <- tapply(data, list(f1, f2), mean)
if (any(is.na(cellmeans)))
stop("Incomplete combinations of factors!")
xr <- c(1, nlevels(f2))
yr <- range(cellmeans)
o.par <- par(xaxt = "n")
on.exit(par(o.par))
plot(0, 0, type = "n", xlim = xr, ylim = yr, main = main,
xlab = xlab, ylab = ylab)
apply(cellmeans, MARGIN = 1, function(x) {
lines(x)
invisible()
})
text(xr[2], cellmeans[, ncol(cellmeans)], rownames(cellmeans),
pos = 4)
invisible(list(cellmeans = cellmeans))
}
--
Kaspar Pflugshaupt
Geobotanisches Institut ETH Zurich
mailto: pflugshaupt at geobot.umnw.ethz.ch
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://stat.ethz.ch/pipermail/r-help/attachments/20000509/4a1eb501/attachment.html
More information about the R-help
mailing list