Dennis, thank you for the response!
Sorry for lack of clarity, I'll explain a little more below...

> plot(Depth[LithClass=='sand'], Conductivity[LithClass=='sand'])
>> (ad nauseum... how can I loop through them all?)
>>
>
I have several lithology classes - sand, clay, limestone, etc... I wish to
perform this plot (and boxplots) for each lith class, i.e.:
> plot(Depth[LithClass=='sand'], Conductivity[LithClass=='sand'])
> plot(Depth[LithClass=='clay'], Conductivity[LithClass=='clay'])
etc. I told you I was new to R :)


> One way:
> sand <- subset(tc, LithClass == 'sand')
> plot(Conductivity ~ Depth, data = sand, ...)
>
> this will still only generate one plot for 'sand'.  To get a plot for each
lith class I figure (from other programming languages) that i'd have to
construct a loop of some sort. I've since worked this one out:

lc=list('clay','coal','sand','marl','limestone')

for (i in 1:length(lc)){
    # set output file
    setEPS()    # sets many postscript options suitable for EPS output
    postscript(paste('boxplot-',lc[i],'.eps',sep=''))
    boxplot(Conductivity[LithClass==lc[i]]) # need to play with axes though
    title(main=lc[i], ylab='tc')
    dev.off()
}

This seems very procedural, but it's what I'm used to.

If you made it clear what you were really after, one could be more
> forthcoming, but if the idea is to plot all pairs of variables within a
> particular data subset, there are functions in the apply family and in
> package plyr that can help you in that regard.  Here's one approach for the
> individual boxplots, applied to the clay LithClass:
>
> vars <- names(which(sapply(tc, is.numeric)))   # select numeric vars
> clay <- subset(tc, LithClass == 'clay')
>
> # Using the plot function
>
> g <- function(x) {
>           with(clay, boxplot(eval(parse(text = x)), xlab =
> eval(substitute(x))))
>           Sys.sleep(1)
>       }
> # (1): loop
> for(i in seq_along(vars)) g(vars[i])
> # (2): Use lapply in place of the loop:
> lapply(vars, g)
>
> # For all x-y scatterplots, one approach is via the m_ply() function
> # in package plyr. A more efficient means of  choosing x and y in
> # expand.grid() is certainly possible, though.
>
> library(plyr)
> vpairs <- expand.grid(x = vars, y = vars)
> vpairs <- vpairs[vpairs$x != vpairs$y, ]
>
> # Basic plot function:
> f <- function(x, y)  {
>      fm <- as.formula(paste(y, x, sep = '~'))
>      plot(fm, data = clay)
>      Sys.sleep(1)
>    }
> # Let the show begin:
> m_ply(vpairs, f)
>
> It's easier to do all of this when the plot function has a formula
> interface. Of course, the pairs() function or one of its variants among
> multiple packages may well be a more efficient approach.
>
> If you can split the data into multiple subsets, you can wrap up some or
> all of the above in a function and call it with lapply() in the base
> package. I'll leave that as a homework exercise :)
>
> It's going to take me some time to read up on all these new functions
you've introduced me to.

Thanks again,
ben.

        [[alternative HTML version deleted]]

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to