﻿ Resources > R Code samples > Normal distribution plot

# Normal distribution plot

Navigation:  Resources > R Code samples >

# Normal distribution plot

Create sample Normal distribution plots with the upper one-tail (2.5%) of the distribution highlighted in blue and the two-tails (giving 5% of the distribution at +/-1.96 standard deviations from the mean). Code based on that provided in Crawley (2007, p148).

R Code

## define a 1x2 grid for plotting

par(mfrow=c(1,2))

## define the x range for evaluating the distribution

x<-seq(-3,3,0.01)

## set f to the normal distribution function at the selected points

f=dnorm(x)

##plot the distribution and add polygons colored to identify the two-tailed and one-tailed versions

plot(x,f,type="l")

polygon(c(x[x>=-3],3),c(f[x>=-3],f[x==3]),col="gray")

polygon(c(x[x>=1.96],1.96),c(f[x>=1.96],f[x==3]),col="blue")

polygon(c(x[x<=-1.96],-1.96),c(f[x<=-1.96],f[x==-3]),col="blue")

plot(x,f,type="l")

polygon(c(x[x>=-3],3),c(f[x>=-3],f[x==3]),col="gray")

polygon(c(x[x>=1.96],1.96),c(f[x>=1.96],f[x==3]),col="blue")