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

John Kane jrkrideau at inbox.com
Mon May 27 15:08:17 CEST 2013


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.wilding at sams.ac.uk
> Sent: Mon, 27 May 2013 10:39:58 +0000
> 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,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 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.

____________________________________________________________
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.



More information about the R-help mailing list