Question

I have the following R script which produces the following image. I'd like to add SD regions to this normal curve (plus or minus 3 SD). In other words, two lines indicating the 68% region, and so on. You can see an example of the lines I'm talking about here.

g = c(1,2,3,4,5,6,7,8,9,2,3,3,3,4,4,4,5,5,6,6,6,6,6,7,8)
h<-hist(g, breaks=10, density=10, col="lightgray", xlab="Accuracy", main="Overall") 
xfit<-seq(min(g),max(g),length=40) 
yfit<-dnorm(xfit,mean=mean(g),sd=sd(g)) 
yfit <- yfit*diff(h$mids[1:2])*length(g) 
lines(xfit, yfit, col="black", lwd=2)

enter image description here

Was it helpful?

Solution

You need to use ?segments. Try this:

x.vals <- c(mean(g)-(1:3*sd(g)), mean(g), mean(g)+(1:3*sd(g)))
y.vals <- dnorm(x.vals, mean=mean(g),sd=sd(g))*diff(h$mids[1:2])*length(g)
segments(x0=x.vals, y0=0, x1=x.vals, y1=y.vals)

enter image description here

Note that not all SDs show up on the plot. To make the original plot wider, you could use xlim=c(-3,13) in the call to hist() (and you'd also need to change the range of xfit accordingly).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top