# [S] newbie question

Roy Pardee (rpardee@netscape.net)
27 Nov 98 16:33:10 PST

Greetings all,

I'm trying to write a simple splus function (version 3.3
running under Win95) & suspect I'm taking a wrong approach.
I wonder if one or more of you nice gurus out there would
help to put me on the right track?

The function makes a multiple line plot of a statistic over
a variable, broken out by a second variable (for instance,
mean income over time broken out by sex).

So far I've come up with the following, which sort of works:

statplot <- function(response, abscissa, grouper, statistic)
{
stats <- tapply(response, list(abscissa, grouper), statistic)
# Set the axes
plot(matrix(stats, ncol = 1),
type = "n",
xlim = c(1, nrow(stats)),
xlab = attributes(abscissa)\$name,
ylab = "statistic"
)
# Plot each series of stats
for(i in 1:ncol(stats))
{
lines(stats[, i],
type = "o",
lty = i,
pch = i,
lwd = 2)
}
}

# Create a test dataset
testy <- data.frame(income = runif(60)*10 + 300,
wave = rep(1:6,10),
site = c(rep("San Mateo", 20),
rep("Tucson", 20),
rep("Seattle", 20)
)
)
# Test out the function
attach(testy)
statplot(income, wave, site, mean)

This works fairly well, but I have two problems. First, the
axis labels are not being set properly--for the x label,
attributes(abscissa)\$name resolves to "1" rather than the
name of the object specified for the abscissa argument.
Similarly, the y label just comes to the literal string
"statistic". (If I use {xlab = statistic}, then the entire
<statistic> function gets passed to xlab & an error occurrs.
If I try {xlab = names(statistic)} then the names of the
arguments that <statistic> takes get passed.)

So... is there some function that returns the name of a
specified object as a character string, so that
namefunc(income) would return "income"?

Second, and more importantly, the plots that get generated
by lines() are actually of the statistic against the row
index, rather than the values of the variable specified as
abscissa. This is fine so long as the actual abscissa
variable has every value from one to max(abscissa) (which is
so for my immediate need) but that's obviously a pretty
nasty limitation. So, can anyone reccomend a better
approach, one that actually makes the plots I want directly
(as opposed to faking it, as statplot() currently does)?

summarize for the list.