tylerdurdenX
tylerdurdenX

Reputation: 21

R Programming Normal Distribution

enter image description here

I am trying to create this plot. When I tried the code below plot is not distributed normally.

x <- seq(60,140,20)
y <- dnorm(x,0,1)
plot(x,y,type="l",xlab = "x",ylab = "f(x)",main = "The total shades area is equal to 0.05")
x<-seq(60,80)
y<-dnorm(x,0,1)
polygon(c(60,x,80),c(0,y,0),col="red")
x<-seq(120,140,length=100)
y<-dnorm(x,0,1)
polygon(c(120,x,140),c(0,y,0),col="red")

Upvotes: 2

Views: 451

Answers (1)

Lynn L
Lynn L

Reputation: 523

It looks like the biggest issue with your plot is that, based on your image, you want to graph a normal distribution with mean 100 and standard deviation 10, but anytime you call dnorm, you're using a mean of 0 and a standard deviation of 1.

A secondary issue is that when you're defining the first sequence x, you have the by argument equal to 20, which means you're only using 60, 80, 100, 120, and 140 as the x coordinates for your normal curve. Try using something smaller--like 1--instead.

The code below should return the graph you're looking for.

x <- seq(60,140,1)
y <- dnorm(x,100,10)
plot(x,y,type="l",xlab = "x",ylab = "f(x)",
     main = "The total shaded area is equal to 0.05")
x<-seq(60,80)
y<-dnorm(x,100,10)
polygon(c(60,x,80),c(0,y,0),col="red")
x<-seq(120,140,length=100)
y<-dnorm(x,100,10)
polygon(c(120,x,140),c(0,y,0),col="red")

Upvotes: 8

Related Questions