[R] Creating multiple graphs based on one variable

Tim Clark mudiver1200 at yahoo.com
Tue May 26 11:34:55 CEST 2009


Dear List,

I would like to create several graphs of similar data.  I have x and y values for several different individuals (in this case fish).  I would like to plot the x and y values for each fish separately.  I can do it using a for loop, but I think I should be using "apply".  Please let me know what I am doing wrong, or if there is a "better" way to do this.  What I have is:

#Test data
dat<-data.frame(c(rep(1:10,4)),c(rep(1:10,4)),c(rep(c("Tony","Mike","Vicky","Fred"),each=10)))
names(dat)<-c("x","y","Name") 

#Create function to plot x and y
myplot<-function() plot(dat$x,dat$y)

#Apply the function to each of the names
par(mfcol=c(2,2))   
apply(dat,2,myplot,by=dat$Name) #Does not work - tried various versions

I would like separate plots for Tony, Mike, and Vicky.  What is the best way to do this?  

Thank!

Tim


Tim Clark
Department of Zoology 
University of Hawaii




More information about the R-help mailing list