How can I plot a uniform distribution in R? 2

How can I plot a uniform distribution in R?

To plot a uniform distribution in R, one can use the built-in function “runif” which generates a set of random numbers with a uniform distribution. This function takes in the parameters for the minimum and maximum values, as well as the number of observations. Once the random numbers are generated, they can be plotted using the “hist” function, which creates a histogram displaying the distribution of the data. Additionally, the “curve” function can be used to plot the probability density function of the uniform distribution. By adjusting the parameters and repeating the process, one can visualize different variations of the uniform distribution.

Plot a Uniform Distribution in R


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

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

#calculate uniform distribution probabilities
y <- dunif(x, min = -3, max = 3)

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

#calculate uniform distribution probabilities
y <- dunif(x, min = -3, max = 3)

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

Cite this article

stats writer (2024). How can I plot a uniform distribution in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-plot-a-uniform-distribution-in-r/

stats writer. "How can I plot a uniform distribution in R?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-plot-a-uniform-distribution-in-r/.

stats writer. "How can I plot a uniform distribution in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-plot-a-uniform-distribution-in-r/.

stats writer (2024) 'How can I plot a uniform distribution in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-plot-a-uniform-distribution-in-r/.

[1] stats writer, "How can I plot a uniform distribution in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I plot a uniform distribution in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top