Home » How to Plot a Uniform Distribution in R

How to Plot a Uniform Distribution in R

by Tutor Aspire

The uniform distribution is a probability distribution in which every value between an interval from a to b is equally likely to occur.

If a random variable X follows a uniform distribution, then the probability that X takes on a value between x1 and x2 can be found by the following formula:

P(x1 2) = (x2 – x1) / (b – a)

where:

  • x1: the lower value of interest
  • x2: the upper value of interest
  • a: the minimum possible value
  • b: the maximum possible value

The following examples show how to plot a uniform distribution in R.

Example 1: Plot Basic Uniform Distribution in R

The following code shows how to plot a basic uniform distribution in R:

#define x-axis
x #calculate uniform distribution probabilities
y #plot uniform distribution
plot(x, y, type = 'l')

The x-axis displays the potential values for a random variable that follows a uniform distribution while the y-axis shows the probability that the random variable takes on those values.

Note: The dunif() function in R is used to calculate the density of a uniform distribution, given a minimum and maximum value.

Example 2: Plot Custom Uniform Distribution in R

The following code shows how to plot a basic uniform distribution in R along with how to modify the title, axes labels, and colors:

#define x-axis
x #calculate uniform distribution probabilities
y #plot uniform distribution
plot(x, y, type = 'l', lwd = 3, ylim = c(0, .2), col='blue',
     xlab='x', ylab='Probability', main='Uniform Distribution Plot')

Additional Resources

The following tutorials explain how to plot other distributions in R:

How to Plot a Normal Distribution in R
How to Plot a Chi-Square Distribution in R
How to Plot a Poisson Distribution in R
How to Plot a Binomial Distribution in R
How to Plot an Exponential Distribution in R

You may also like