<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Ko-Kang Wang wrote:
<blockquote TYPE=CITE>Is there a function to do interaction plots in R?
<br>I know in Splus I can simply use interaction.plot()
<p>Thanks.</blockquote>
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).
<p>Here's the code; use it at your own risk :-)
<p>Kaspar
<p>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.
<br>&nbsp;
<p>int.plot &lt;- function (f1, f2, data, main = "Interaction Plot", xlab
= deparse(substitute(f2)),
<br>&nbsp;&nbsp;&nbsp; ylab = deparse(substitute(f1)), ...)
<br>{
<br>&nbsp;&nbsp;&nbsp; cellmeans &lt;- tapply(data, list(f1, f2), mean)
<br>&nbsp;&nbsp;&nbsp; if (any(is.na(cellmeans)))
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stop("Incomplete combinations
of factors!")
<br>&nbsp;&nbsp;&nbsp; xr &lt;- c(1, nlevels(f2))
<br>&nbsp;&nbsp;&nbsp; yr &lt;- range(cellmeans)
<br>&nbsp;&nbsp;&nbsp; o.par &lt;- par(xaxt = "n")
<br>&nbsp;&nbsp;&nbsp; on.exit(par(o.par))
<br>&nbsp;&nbsp;&nbsp; plot(0, 0, type = "n", xlim = xr, ylim = yr, main
= main,
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xlab = xlab, ylab = ylab)
<br>&nbsp;&nbsp;&nbsp; apply(cellmeans, MARGIN = 1, function(x) {
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lines(x)
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invisible()
<br>&nbsp;&nbsp;&nbsp; })
<br>&nbsp;&nbsp;&nbsp; text(xr[2], cellmeans[, ncol(cellmeans)], rownames(cellmeans),
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pos = 4)
<br>&nbsp;&nbsp;&nbsp; invisible(list(cellmeans = cellmeans))
<br>}
<br>&nbsp;
<pre>--&nbsp;
Kaspar Pflugshaupt
Geobotanisches Institut ETH Zurich
mailto: pflugshaupt@geobot.umnw.ethz.ch</pre>
&nbsp;</html>