问题
I have the following code, in R.
x = c(rep(2,10),rep(4,10))
y1 = c(5.1,3,4.2,4.1,4.8,4.0,5,4.15,3,4.5)
y2 = c(9.1,8,9.2,8.2,7,9.5,8.8,9.3,10,10.4)
y = c(y1,y2)
plot(x,y,pch=16,cex=0.9,xlim=c(0,6),ylim=c(0,13))
This code produces a plot with two bands of dots. I've overlayed normal curves sideways on those bands using powerpoint. How can I do this in R (drawing the sideways normal curves), using the actual means and sd values? NOTE: I repeat, the normal curves are not part of the plot. The code above just produces the raw plot.
 
    回答1:
First, calculate mean and standard deviation for y1 and y2.
m1<-mean(y1)
s1<-sd(y1)
m2<-mean(y2)
s2<-sd(y2)
Then made two data frame (for convenience) that contains y values as sequence of numbers (wider than actual y1 and y2 values). Then calculated density values for x using dnorm() and calculated mean and standard deviation values. Then added 2 or 4 to shift values to 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 function lines().
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))
 
    来源:https://stackoverflow.com/questions/15142374/drawing-overlayed-sideways-plots-in-r