Skip to content

using lapply and split to plot up subsets of a vector

3 messages · karmakiller, Phil Spector

#
Hi,

I would like to be able to plot data from each of the sp.id on individual
plots. At the moment I can plot all the data on one graph with the following
commands but I cannot figure out how to get individual graph for each sp.id.

i<- function(df)plot(lnbm,ln.o2con,data=df)
j<- lapply(split(one,one$sp.id),i)

I have searched on the net and through the threads here but I cannot find
anything that matches what I am trying to do. Any help would be greatly
appreciated.

Thanx
#
The data= argument to plot only makes sense if the first 
argument is a formula.  So if you change the plot command
in your function to

        plot(ln.o2con~lnbm,data=df)

you might get what you want.  But I would suggest you take a
look at the plot produced by

library(lattice)
xyplot(ln.o2con~lnbm|sp.id,data=one)

which might be more useful.

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu
On Tue, 28 Dec 2010, karmakiller wrote:

            
1 day later
#
Hi again,

I have spent the last couple of days trying to build a function that will
allow me to add to the multiple plots that I created with your advice. I
have changed to 

plot(ln.o2con~lnbm,data=df) 

in my function and this works fine. 

On an individual plot I can fit quantile regressions using "quantreg" but I
can't figure out how to plot these lines on the multiple graphs that I can
create with plot.

a<- function(df)abline(rq(ln.o2con~lnbm,tau=0.99,data=df))
b<- lapply(split(one,one$sp.id),a)
print(b)
c<- function(df)abline(rq(ln.o2con~lnbm,tau=0.01,data=df))
d<- lapply(split(one,one$sp.id),c)
print(d)
e<- function(df)abline(rq(ln.o2con~lnbm,tau=0.01,data=df))
f<- lapply(split(one,one$sp.id),e)
print(f)

My knowledge of building functions in R is poor and I can't figure out how
to combine these functions so that I may plot each graph individually with
the correct ablines for each fit. I can get the lines to plot but only on
one graph.

Again I would be grateful for any help, I really like using R but it is the
bottleneck in my analysis and things are moving very slow while I try to
tackle this steep learning curve and become a more familiar with how to use
R the best way.

Thank you