legend with lines and points

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

legend with lines and points

R Kozarski
Hi, say there are x and y given as:

level x y
3 0.112 0.012
2 0.432 0.111
1 0.415 0.053
3 0.38 0.005
2 0.607 0.01
1 NA NA
3 0.572 0.01
2 0.697 0.039
1 0.377 0.006
3 NA NA
2 0.571 0.003
1 0.646 0.014
3 0.063 0.024
2 0.115 0.017
1 0.035 0.042
3 0.426 0

I did the following plot:

plot(y ~ x, pch=c(1,2,3), col=c('red', 'green', 'blue'));
abline(lm(y ~ x), col='red');
lines(lowess.na(y,x), col='blue')
abline(lm(y~x),col='red')
legend('topright',c('Top','Middle','Bottom'), col=c('red', 'green', 'blue'),pch=c(1,2,3))
legend('right',c('linear','LOWESS'), col=c('red','blue'),lty=c(1,2))

where:
lowess.na <- function(x, y) {  #do lowess with missing data
    x1 <- subset(x,(!is.na(x)) &(!is.na(y)))
  y1 <- subset(y, (!is.na(x)) &(!is.na(y)))
  lowess.na <- lowess(x1~y1)
  }

I want ONE legend to involve points (empirical) and lines from linear and lowess fit together. I guess it is simple but.....

best, robert