# Re: Plots for subsets of data

Wed, 21 Jan 1998 11:06:19 +1030

Jeff Morris writes:
>
> I have three variables, one categorical and two numeric. I
> want to plot the numeric ones against eachother for each
> unique value of the categorical variable. I could use coplot,
> but I want separate plots. Now, I did figure out how to do
> it, but surely there is an easy way. I was hoping for
> something like plot(x, y, cat="a") rather than going through
> the bother of subsetting both x and y. Can anyone tell me if
> there is a simple way to do this? In general, is there a
> simple way to subset data by a variable then run a function on
> it?
>
> Thanks!

(Hmm. Why do I suspect Jeff might be a SAS refugee... :-)

I suppose the way you figured out to do it might be

> con <- cat == "a"
> plot(x[con], y[con], xlab = "x", ylab = "y", main = "cat = a")

in which case I can't see all that much of a problem. There are
other possibilities, though.

Suppose x, y and cat are all part of a data frame, dat. If not,
make one.

1. With Trellis graphics you could try

> xyplot(y ~ x, dat, subset = cat == "a")

2. With conventional graphics you could use

> attach(dat[dat\$cat == "a", ])
> plot(x, y, main = "cat = a")
> detach()

However this assumes you have no separate variables called x, y
or cat in position 1 of the search path. If you do have those,
you need to attach to, and detach from postion 1.

Bill Venables.

```--
Bill Venables, Head, Dept of Statistics,    Tel.: +61 8 8303 5418
University of Adelaide,                     Fax.: +61 8 8303 3696