Skip to content

Interaction Plots in R

10 messages · Kaspar Pflugshaupt, Faheem Mitha, Martin Maechler +3 more

#
Is there a function to do interaction plots in R?
I know in Splus I can simply use interaction.plot()

Thanks.

--
-----------------------------------------------------------------------------------

 Ko-Kang Wang
 Undergraduate Student
 Computer Science/Statistics Double Major
 University of Auckland
 Auckland 1005
 New Zealand
-----------------------------------------------------------------------------------



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Ko-Kang Wang wrote:

            
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
14 days later
#
Well, I've been using that function a lot lately and so I've improved it
considerably, up to the point of being useful. Having a bit of a bad
conscience for my last version (it was rather ugly), I'd like to post a
better one. It uses colours, draws a legend (which can be reordered) and
does a better job with axis labels. What's more, I've since discovered the
"matlines" function :-) .

If anyone else needs interaction plots similar to S-Plus, here you go:

----------------------------------------------------------
interaction.plot<-
function (f1, f2, data, main = "Interaction Plot",
    xlab = deparse(substitute(f2)),
    ylab = paste("Mean of", deparse(substitute(data))),
    legend=T, leg.order = 1:n1, ...)
{

    # f1,f2: factors. data: data vector
    # (extraction from matrix or data.frame not implemented)
    
    cellmeans <- tapply(data, list(f1, f2), mean)
    n1 <- nlevels(f1)
    n2 <- nlevels(f2)
    xr <- c(1, n2)
    yr <- range(cellmeans)
    plot(0, 0, type = "n", xaxt = "n", xlim = xr, ylim = yr,
        main = main, xlab = xlab, ylab = ylab)
    matlines(t(cellmeans), lty = 1, col = 1:n1)
    axis(1, at = 1:n2, labels = as.character(levels(f2)))
    if(legend)    
        legend(locator(1), legend = rownames(cellmeans)[leg.order],
            lty = 1, col = (1:n1)[leg.order])

    # locator(1) waits for user to choose legend location
    # (click marks upper left corner)
    # legend entries get ordered according to leg.order

    invisible(list(cellmeans = cellmeans))
    # cellmeans are returned
}
--------------------------------------------------------

 -- 
Kaspar Pflugshaupt
Geobotanisches Institut ETH Zurich
mailto: pflugshaupt at geobot.umnw.ethz.ch
#
Ah yes.  I've just tested the code and it proved to be extremely good.  Thank you.

I actually think it will be a good idea to put up a package (library), with
examples and help files etc...I don't know how the others on this list think about
this idea, but I would certainly like to see such a package in CRAN.

If you don't mind, I could put up a package for you and e-mailed it to you.

Thanks again.

Ko-Kang
Kaspar Pflugshaupt wrote:

            
--
-----------------------------------------------------------------------------------

 Ko-Kang Wang
 Undergraduate Student
 Computer Science/Statistics Double Major
 University of Auckland
 Auckland 1005
 New Zealand
-----------------------------------------------------------------------------------



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
I think it would be extremely useful to have code for interaction
plots. This is something I missed when working with linear models on
R. But why not add it to the R-base?          
                                             Faheem.
On Wed, 24 May 2000, Ko-Kang Wang wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Faheem> I think it would be extremely useful to have code for interaction
    Faheem> plots. This is something I missed when working with linear models on
    Faheem> R. But why not add it to the R-base?          

yes. definitely.  This should go into R base, -- unless Kaspar objects --
and I think for 1.1.0 (due around June 15).

Kaspar (or anyone), can you provide the corresponding help (source) file,
interaction.plot.Rd -- including a few examples {using R builtin datasets} ?

We then will put it into R "base".

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO D10	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
Faheem> On Wed, 24 May 2000, Ko-Kang Wang wrote:
>> Ah yes.  I've just tested the code and it proved to be extremely
    >> good.  Thank you.
    >> 
    >> I actually think it will be a good idea to put up a package
    >> (library), with examples and help files etc...I don't know how the
    >> others on this list think about this idea, but I would certainly
    >> like to see such a package in CRAN.
    >> 
    >> If you don't mind, I could put up a package for you and e-mailed it
    >> to you.
    >> 
    >> Thanks again.
    >> 
    >> Ko-Kang
    >>
>> Kaspar Pflugshaupt wrote:
>> 
    >> > >>
>> > >> 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.
    >> >
    >> > Well, I've been using that function a lot lately and so I've improved it
    >> > considerably, up to the point of being useful. Having a bit of a bad
    >> > conscience for my last version (it was rather ugly), I'd like to post a
    >> > better one. It uses colours, draws a legend (which can be reordered) and
    >> > does a better job with axis labels. What's more, I've since discovered the
    >> > "matlines" function :-) .


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Faheem Mitha wrote:
Well, I'm not sure that it is ready for that. For example, it has no
protection against wrong parameter types and cannot extract factors from
a dataframe (as the S-Plus version does). Compatibility to the S-Plus
version should be checked thoroughly, as a matter of fact. Generally,
much polish is missing, and I will probably not have the time to finish
it properly. Anybody who wants to do it is most welcome!

Meanwhile, if Ko-Kang Wang makes a package of it, that's enough for my
needs.

Kaspar
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Well, as I said.  I have actually put it into a package which has a help file and an
example.

I agree with Kaspar, it may not be ready to put into R base.  I will send the package file
to Kaspar in an hour or so as I've almost finished putting them together (now I'm just doing
some final checking), and if he decides to put it up, anyone who is interested can download
it, improve it (both the code and help files!), then perhaps later on it can be placed into
R Base.

I am just curious why nobody brought this interaction plot idea up, since R has be around
for quite a few years!

Cheers,

Ko-Kang
Martin Maechler wrote:

            
--
-----------------------------------------------------------------------------------
 Ko-Kang Wang
 Undergraduate Student
 Computer Science/Statistics Double Major
 University of Auckland
 Auckland 1005
 New Zealand
-----------------------------------------------------------------------------------


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
The current plan is to put an S-compatible version of interaction.plot
into 1.1.0.
On Wed, 24 May 2000, Ko-Kang Wang wrote:

            
I think that shows how little interaction plots are used.  The problem I
find is that they are sometimes useful for two-way layouts, but I rarely
see such simple designs.  One often needs to plot particular
two-way interactions as part of a larger model.

Another problem is that they depend crucially on the ordering of the
levels in factors, so apart from ordered factors the order in which levels
are joined up is completely arbitrary.
#
Prof Brian D Ripley <ripley at stats.ox.ac.uk> writes:
...
..or how easy it often is to get a plot with equivalent information
with matlines(). I actually hadn't noticed the existence of
interaction.plot() before.

We might need a general "trace grapher" including continuous variables
instead of factors and handling unbalanced designs, e.g. plot data vs.
time for each person, with different symbols for different groups.
Maybe also with fixups for missing values, etc. (Now don't tell me
such a thing already exists!)