简体   繁体   中英

How to draw normal distribution graph with two standard deviation in R

I am new in R and would like to plot a normal distribution graph where the region of two standard deviation is selected by arrows, exactly as shown below.

在此处输入图片说明

This question lacks the effort but it piqued my interest, so there you have it:

#standard normal distribution data
x <- seq(-4, 4, length=100)
hx <- dnorm(x)

#plot a standard normal distribution
plot(x, hx, type="l", lty=2, xlab="x value")
#plot a vertical line at -2*std
abline(v=-2, col='red')
#plot a vertical line at  2*std
abline(v= 2, col='red')
#make the arrow
arrows(x0=-2, y0=0.35, x1=2, y1=0.35, code=3, col='blue')
#plot the text
text(x=0, y=0.37,labels='95%', col='red')

Result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM