How to plot a normal distribution by labeling specific parts of the x-axis?

后端 未结 7 1845
轻奢々
轻奢々 2020-12-03 00:01

I am using the following code to create a standard normal distribution in R:

x <- seq(-4, 4, length=200)
y <- dnorm(x, mean=0, sd=1)
plot(x, y, type=\"         


        
7条回答
  •  借酒劲吻你
    2020-12-03 00:25

    This is how to write it in functions:

    normalCriticalTest <- function(mu, s) {
      x <- seq(-4, 4, length=200) # x extends from -4 to 4
      y <- (1/(s * sqrt(2*pi))) * exp(-((x-mu)^2)/(2*s^2)) # y follows the formula 
    of the normal distribution: f(Y)
      plot(x,y, type="l", lwd=2, xlim = c(-3.5,3.5))
      abline(v = c(-1.96, 1.96), col="red") # draw the graph, with 2.5% surface to 
    either side of the mean
    }
    normalCriticalTest(0, 1) # draw a normal distribution with vertical lines.
    

    Final result:

提交回复
热议问题