To plot a normal distribution in R, we can either use base R or install a fancier package like ggplot2.
Using Base R
Here are three examples of how to create a normal distribution plot using Base R.
Example 1: Normal Distribution with mean = 0 and standard deviation = 1
To create a normal distribution plot with mean = 0 and standard deviation = 1, we can use the following code:
#Create a sequence of 100 equally spaced numbers between -4 and 4 x #create a vector of values that shows the height of the probability distribution #for each value in x y #plot x and y as a scatterplot with connected lines (type = "l") and add #an x-axis with custom labels plot(x,y, type = "l", lwd = 2, axes = FALSE, xlab = "", ylab = "") axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))
This generates the following plot:
Example 2: Normal Distribution with mean = 0 and standard deviation = 1 (less code)
We could also create a normal distribution plot without defining x and y, and instead simply using the “curve” function using the following code:
curve(dnorm, -3.5, 3.5, lwd=2, axes = FALSE, xlab = "", ylab = "") axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))
This generates the exact same plot:
Example 3: Normal Distribution with customized mean and standard deviation
To create a normal distribution plot with a user-defined mean and standard deviation, we can use the following code:
#define population mean and standard deviation population_mean #define upper and lower bound lower_bound #Create a sequence of 1000 x values based on population mean and standard deviation x #create a vector of values that shows the height of the probability distribution #for each value in x y #plot normal distribution with customized x-axis labels plot(x,y, type = "l", lwd = 2, axes = FALSE, xlab = "", ylab = "") sd_axis_bounds = 5 axis_bounds
This generates the following plot:
Using ggplot2
Another way to create a normal distribution plot in R is by using the ggplot2 package. Here are two examples of how to create a normal distribution plot using ggplot2.
Example 1: Normal Distribution with mean = 0 and standard deviation = 1
To create a normal distribution plot with mean = 0 and standard deviation = 1, we can use the following code:
#install (if not already installed) and load ggplot2 if(!(require(ggplot2))){install.packages('ggplot2')} #generate a normal distribution plot ggplot(data.frame(x = c(-4, 4)), aes(x = x)) + stat_function(fun = dnorm)
This generates the following plot:
Example 2: Normal Distribution using the ‘mtcars’ dataset
The following code illustrates how to create a normal distribution for the miles per gallon column in the built-in R dataset mtcars:
ggplot(mtcars, aes(x = mpg)) + stat_function( fun = dnorm, args = with(mtcars, c(mean = mean(mpg), sd = sd(mpg))) ) + scale_x_continuous("Miles per gallon")
This generates the following plot: