[R] Subset and plot

Chuck Cleland ccleland at optonline.net
Tue Feb 2 21:15:44 CET 2010


On 2/2/2010 2:51 PM, Marlin Keith Cox wrote:
> Here is a runable program.  When I plot Day and Wgt, it graphs all the data
> points.  All I need is daily.sub1 plotted.  I also need each "Tanks" to have
> its own col or pch.  When I run it with the line with pch, it gives me
> nothing.

DF <- data.frame(Trial=rep(c(1,2),each=12),
                 Tanks=rep(c("a3","a4","c4","h4"),each=3,2),
                 Day=rep(c(1:12),2),
                 Wgt=c(1:24))

with(subset(DF, Trial==2 & Tanks %in% c("a4",'c4','h4')),
     plot(Day, Wgt, pch=as.numeric(Tanks)))

library(lattice)

trellis.device(new=TRUE, color=FALSE)

xyplot(Wgt ~ Day, groups=Tanks,
       data=subset(DF, Trial==2 & Tanks %in% c("a4",'c4','h4')),
       par.settings=list(superpose.symbol=list(pch=c(2,19,21))))

> rm(list=ls())
> Trial<-rep(c(1,2),each=12)
> Tanks=rep(c("a3","a4","c4","h4"),each=3,2)
> Day=rep(c(1:12),2)
> Wgt=c(1:24)
> daily<-cbind(Trial, Tanks, Day, Wgt)
> daily
> daily.sub<-subset(daily, subset=Trial==2 & Tanks=="a4"|Trial==2 &
> Tanks=="c4"|Trial==2 & Tanks=="h4")
> daily.sub1<-as.data.frame(daily.sub)
> attach(daily.sub1)
> daily.sub1
> x11()
> plot(Day, Wgt)
> #plot(Day, Wgt, pch=c(2,19,21)[Tanks])
> detach(daily.sub1)

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list