How to Plot a Normal Distribution in R


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 <- seq(-4, 4, length=100)

#create a vector of values that shows the height of the probability distribution
#for each value in x
y <- dnorm(x)

#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 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 <- 50
population_sd <- 5

#define upper and lower bound
lower_bound <- population_mean - population_sd
upper_bound <- population_mean + population_sd

#Create a sequence of 1000 x values based on population mean and standard deviation
x <- seq(-4, 4, length = 1000) * population_sd + population_mean

#create a vector of values that shows the height of the probability distribution
#for each value in x
y <- dnorm(x, population_mean, population_sd)

#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 <- seq(-sd_axis_bounds * population_sd + population_mean,
                    sd_axis_bounds * population_sd + population_mean,
                    by = population_sd)
axis(side = 1, at = axis_bounds, pos = 0)

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:

Leave a Reply

Your email address will not be published. Required fields are marked *