plot in R

Mar. 26th, 2010 04:39 pm
gusl: (Default)
[personal profile] gusl
I'm trying to make a plot like this. Any ideas?

Is this the answer?

Date: 2010-03-27 06:17 am (UTC)
From: [identity profile] serapio.livejournal.com
N = 5
pts = data.frame(Ax = runif(N), Bx = runif(N), Cx = runif(N))
pts$Ay = rnorm(1,sd=.5)*pts$Ax^2 + rnorm(1,sd=.1)*pts$Ax + rnorm(1,8)
pts$By = rnorm(1,sd=.5)*pts$Bx^2 + rnorm(1,sd=.1)*pts$Bx + rnorm(1,4)
pts$Cy = rnorm(1,sd=.5)*pts$Cx^2 + rnorm(1,sd=.1)*pts$Cx + rnorm(1,0)
all = unlist(pts)
dim(all) = c(N*3,2)

# Set up bounds
plot(all,type='n')

# Draw black regression lines
regx = seq(0,1,len=100)
for (idx in 1:3) {
x = pts[,idx]
y = pts[,idx+3]
m = lm(y ~ poly(x,2,raw=T))
regy = m$coef %*% rbind(1,regx,regx^2)
lines(regx,regy)
}

# Draw gray connecting lines and colored points
for (idx in 1:N) {
points(pts[idx,1:3], pts[idx,4:6], type='l', col="gray")
points(pts[idx,1:3], pts[idx,4:6], type='p', pch=16, col=c("red","blue","orange"))
}

Re: Is this the answer?

Date: 2010-03-27 09:00 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
wow, I was not expecting 20 lines of code here! Thanks. I didn't realize that you could draw lines using 'points'.

February 2020

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags