First calculate the mean and standard deviation for y1 and y2 .
m1<-mean(y1) s1<-sd(y1) m2<-mean(y2) s2<-sd(y2)
Then he made two data frames (for convenience) that contain y values ββas a sequence of numbers (wider than the actual values ββof y1 and y2 ). Then calculated density values ββfor x using dnorm() and calculated mean and standard deviation values. Then add 2 or 4 to shift the values ββto the desired position.
df1<-data.frame(yval=seq(1,7,0.1),xval=(dnorm(seq(1,7,0.1),m1,s1)+2)) df2<-data.frame(yval=seq(6,12,0.1),xval=(dnorm(seq(6,12,0.1),m2,s2)+4))
Added density lines() with the lines() function.
plot(x,y,pch=16,cex=0.9,xlim=c(0,6),ylim=c(0,13)) with(df1,lines(xval,yval)) with(df2,lines(xval,yval))

source share