[R] error in a abline loop

Elaine Kuo elaine.kuo.tw at gmail.com
Tue Jan 8 02:23:42 CET 2013


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","grey38")[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)
    }




More information about the R-help mailing list