[R] Indexing within by statement - different coloured lines in abline wanted..

Blaser Nello nblaser at ispm.unibe.ch
Mon May 27 15:18:38 CEST 2013


abline(lm(Response1~Predictor,data=Site),col=colours[as.numeric(Site[1,1
])])

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Tom Wilding
Sent: Montag, 27. Mai 2013 12:40
To: r-help at r-project.org
Subject: [R] Indexing within by statement - different coloured lines in
abline wanted..

Dear R-list

I'm trying to get each regression line, plotted using abline, to be of a
different colour as the following code illustrates.  I'm hoping there is
a simple indexing solution.  Many thanks.

## code from here
colours=c("black","red","blue","green","pink")
Mean=500;Sd=10;NosSites=5;Xaxis=seq(1,5,1)
SlopeCoefficient=5;Site=(gl(NosSites,length(Xaxis),labels=1:NosSites))
Predictor=rep(Xaxis,NosSites)
InterceptAdjustment=rnorm(n=NosSites,mean=Xaxis,sd=50)
RandomIntercept=rep(InterceptAdjustment,each=length(Xaxis))
PreResponse=rnorm(n=length(Predictor),
mean=Mean+SlopeCoefficient*1:length(Xaxis),sd=Sd)
Response1=PreResponse+RandomIntercept

#create data frame
Data2=data.frame(Site,Predictor,Mean,SlopeCoefficient,RandomIntercept,Re
sponse1)
Data1=data.frame("Site"=Data2$Site,"Predictor"=Data2$Predictor,"Response
1"=Data2$Response1)
#plotting
var=as.numeric(levels(Data1$Site))
par(mfrow=c(1,3))
plot(Response1~Predictor,data=Data1,xlim=c(min(Xaxis),max(Xaxis)),ylim=c
(MN,MX),
     pch=as.numeric(Site),main="Raw data with linear regresssions by
Site") by(Data1,Data1$Site,function(Site){
  par(new=T)
  abline(lm(Response1~Predictor,data=Site),col=colours[])#index in here.
})
The Scottish Association for Marine Science (SAMS) is registered in
Scotland as a Company Limited by Guarantee (SC009292) and is a
registered charity (9206). SAMS has an actively trading wholly owned
subsidiary company: SAMS Research Services Ltd a Limited Company
(SC224404). All Companies in the group are registered in Scotland and
share a registered office at Scottish Marine Institute, Oban Argyll PA37
1QA. The content of this message may contain personal views which are
not the views of SAMS unless specifically stated. Please note that all
email traffic is monitored for purposes of security and spam filtering.
As such individual emails may be examined in more detail.

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org 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.



More information about the R-help mailing list