The uniform distribution is one of the simplest probability distributions in statistics. It is a type of continuous probability distribution that assumes all outcomes are equally likely. A deck of cards has a uniform distribution because the likelihood of drawing a heart, club, diamond, or spade is equally likely.

In this guide, we’ll take a detailed look at how to plot a uniform distribution in R. We will cover how to generate random numbers following a uniform distribution, how to plot the probability density function (PDF), how to plot the cumulative distribution function (CDF), and how to create a histogram of uniformly distributed data. We’ll also touch on how to overlay a density line on the histogram.

## Step 1: Basic Plotting of Uniform Distribution

R provides built-in functions for the uniform distribution:

`dunif(x, min = 0, max = 1, log = FALSE)`

: Returns the density.`punif(q, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)`

: Returns the distribution function.`qunif(p, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)`

: Returns the quantile function.`runif(n, min = 0, max = 1)`

: Generates random deviates.

Here, `min`

and `max`

are the parameters of the uniform distribution which define the range of the distribution.

Let’s plot the density function of the Uniform distribution. We will use the `curve()`

function in R, which draws a curve representing a function in an interval.

```
# Setting up the parameters
min_val <- 0
max_val <- 1
# Plotting the uniform density
curve(dunif(x, min_val, max_val), from=min_val, to=max_val, ylab="Density", xlab="x",
main=paste("Density of Uniform(", min_val, ",", max_val, ")"))
```

In this code, we’ve defined our range for the Uniform distribution (`min_val`

and `max_val`

), then used the `curve()`

function to draw the density function from the minimum to maximum value. The `dunif()`

function returns the density of the Uniform distribution for different values of `x`

.

The `ylab`

, `xlab`

, and `main`

options in the `curve()`

function are used to set the y-axis label, x-axis label, and the plot title, respectively.

## Step 2: Plotting the Cumulative Distribution Function (CDF)

The cumulative distribution function (CDF) for a random variable is defined as the probability that the variable takes a value less than or equal to a certain value.

The Uniform distribution’s CDF can be plotted similarly to the PDF, using the `punif()`

function:

```
# Plotting the cumulative distribution function
curve(punif(x, min_val, max_val), from=min_val, to=max_val, ylab="CDF", xlab="x",
main=paste("CDF of Uniform(", min_val, ",", max_val, ")"))
```

The `punif()`

function provides the cumulative distribution function of the Uniform distribution.

## Step 3: Generating Uniformly Distributed Random Numbers

To generate uniformly distributed random numbers, we can use the `runif()`

function. Let’s generate 10000 random numbers from a Uniform distribution with a range of 0 to 1:

```
# Generate uniformly distributed random numbers
set.seed(123) # for reproducible results
uniform_random <- runif(10000, min_val, max_val)
```

## Step 4: Creating a Histogram with a Density Line

Once you have generated uniformly distributed random numbers, you can create a histogram to observe the distribution of these numbers:

```
# Create a histogram
hist(uniform_random, prob=TRUE, breaks=40, main="Histogram with density line", xlab="x", ylab="Density")
# Add a density line
lines(density(uniform_random), col="red", lwd=2)
```

In the code above, the `hist()`

function creates a histogram, and the `lines()`

function adds a density line. The `prob=TRUE`

argument in the `hist()`

function indicates that the histogram will represent probabilities instead of counts. The `breaks=40`

argument sets the number of bins in the histogram.

The `density()`

function estimates the density function from the data, and `lines()`

adds this estimated density to the plot.

## Step 5: Overlaying the Theoretical Density

Lastly, you may want to compare the histogram and the estimated density with the theoretical density of the Uniform distribution:

```
# Overlay the theoretical density
curve(dunif(x, min_val, max_val), add=TRUE, col="blue", lwd=2)
```

The `curve()`

function here adds the theoretical density to the existing plot because of the `add=TRUE`

argument.

## Conclusion

In this guide, you’ve learned how to generate and plot a Uniform distribution in R, including how to generate uniformly distributed random numbers, how to plot the probability density function and cumulative distribution function, how to create a histogram, and how to overlay the theoretical and estimated densities.

Remember, a Uniform distribution assumes that all outcomes are equally likely. This assumption can be unrealistic for many real-world phenomena, so the Uniform distribution is often used as a baseline model to compare with other distributions. Still, it’s an important concept in the world of statistics and is also used in areas like computer simulations, Bayesian statistics, and machine learning.