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

2013-05-27 Thread Tom Wilding
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,Response1)
Data1=data.frame(Site=Data2$Site,Predictor=Data2$Predictor,Response1=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@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.


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

2013-05-27 Thread John Kane
Slightly diffferent approach but will this do what you want.

library(ggplot2)
  ggplot(Data1, aes(Predictor, Response1, colour = Site)) +
 geom_smooth(method= lm, se = FALSE) +
 ggtitle(Raw data with linear regresssions by Site)

John Kane
Kingston ON Canada


 -Original Message-
 From: tom.wild...@sams.ac.uk
 Sent: Mon, 27 May 2013 10:39:58 +
 To: r-help@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,Response1)
 Data1=data.frame(Site=Data2$Site,Predictor=Data2$Predictor,Response1=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@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.


Share photos  screenshots in seconds...
TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if1
Works in all emails, instant messengers, blogs, forums and social networks.

__
R-help@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.


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

2013-05-27 Thread Blaser Nello
abline(lm(Response1~Predictor,data=Site),col=colours[as.numeric(Site[1,1
])])

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Tom Wilding
Sent: Montag, 27. Mai 2013 12:40
To: r-help@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@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.

__
R-help@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.