[R] error in a abline loop

PIKAL Petr petr.pikal at precheza.cz
Wed Jan 9 13:50:45 CET 2013


Hi

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of arun
> Sent: Tuesday, January 08, 2013 3:30 PM
> To: Elaine Kuo
> Cc: R help
> Subject: Re: [R] error in a abline loop
> 
> HI Elaine,
> 
> In the data you sent to me, it had 5 levels for skin_color.
> data1<-read.csv("skin_color.csv",sep="\t")
> data1$skin_color<-factor(data1$skin_color)
> levels(data1$skin_color)
> #[1] "1" "2" "3" "4" "5"
> 
> 
>  mypath<-
> file.path("/home/arun/Trial1",paste("Elaine_",1:5,".jpg",sep=""))
> #change the file.path according to your system

Instead of multiple files you can create multipage pdf

pdf("myfile.pdf")

  for(i in 1:5){
#> jpeg(file=mypath[i])
  plot(body_weight~body_length,data=data1[data1$skin_color==i,])
 line<-lm(body_weight~body_length,data=data1[data1$skin_color==i,])
  abline(line,col=c("yellow","blue","green","orange","red")[i],lwd=2)
}
dev.off()

see ?pdf for details

Regards
Petr

#>  }
> 
> #or
> 
> lapply(seq_along(mypath),function(i) {jpeg(file=mypath[i])
>                                      line<-
> lm(body_weight~body_length,data=data1[data1$skin_color==i,])
> 
> plot(body_weight~body_length,data=data1[data1$skin_color==i,])
> 
> abline(line,col=c("yellow","blue","green","orange","red")[i],lwd=2)
>                                      dev.off()
>                                       })
> 
> 
> A.K.
> 
> 
> 
> 
> ----- Original Message -----
> From: Elaine Kuo <elaine.kuo.tw at gmail.com>
> To: arun <smartpink111 at yahoo.com>
> Cc:
> Sent: Monday, January 7, 2013 9:48 PM
> Subject: Re: [R] error in a abline loop
> 
> Hello arun
> 
> Thank you always.
> Please kindly help the attached data for your reference.
> 
> Elaine
> 
> 
> On Tue, Jan 8, 2013 at 10:00 AM, arun <smartpink111 at yahoo.com> wrote:
> > HI,
> >
> > A possible guess ( with no data):
> > for (i in 1:7) {
> >     subs <- data$skin_color==levels(data$skin_color)[i]
> >     line<-lm(body_weight~body_length, data=subset(data, subset=subs),
> >     abline(line,col=c("yellow","chocolate1","darkorange2",
> > "red3","saddlebrown","coral4","grey38")[i],lwd=2) ) #closing
> >parenthesis for lm( was missing
> >     }
> >
> >
> > A.K.
> >
> >
> >
> > ----- Original Message -----
> > From: Elaine Kuo <elaine.kuo.tw at gmail.com>
> > To: r-help at r-project.org
> > Cc:
> > Sent: Monday, January 7, 2013 8:23 PM
> > Subject: [R] error in a abline loop
> >
> > Hello
> >
> > I have data of body length and body weight of people of different
> skin colors.
> >
> > I tried to write a code to plot body length and body weight according
> > to the skin colors.
> > (Thanks for Petr's advice so far.)
> >
> > A loop is used but an error shows up in the following code.
> > It says:
> > unexpected '}' in
> > "
> >"red3","red3","saddlebrown","coral4","chocolate4","darkblue","navy","g
> r
> >ey38")[i],lwd=2)
> >     }"
> >
> > Please kindly advise how to modify the code.
> > Thank you.
> >
> > The code
> >   data <-read.csv("H:/skincolor.csv",header=T)
> >
> >   # graph
> >     par(mai=c(1.03,1.03,0.4,0.4))
> >
> >     plot(data$body_weight, data$body_length,
> >     xaxp=c(0,200,4),
> >     yaxp=c(0,200,4),
> >     type="p",
> >     pch=1,lwd=1.0,
> >     cex.lab=1.4, cex.axis=1.2,
> >     font.axis=2,
> >     cex=1.5,
> >     las=1,
> >     bty="l",col=c("yellow","chocolate1","darkorange2",
> > "red3","saddlebrown","coral4","grey38")[as.numeric(data$skin_color)])
> >
> >
> >
> >#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ~
> >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >     ##
> >     for (i in 1:7) {
> >     subs <- data$skin_color==levels(data$skin_color)[i]
> >     line<-lm(body_weight~body_length, data=subset(data, subset=subs),
> >     abline(line,col=c("yellow","chocolate1","darkorange2",
> > "red3","saddlebrown","coral4","grey38")[i],lwd=2)
> >     }
> >
> > ______________________________________________
> > 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.
> >
> 
> 
> ______________________________________________
> 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