Plot Cumulative Distribution Function in R (original) (raw)

Last Updated : 17 Jul, 2025

A Cumulative Distribution Function (CDF) represents the probability that a random variable takes a value less than or equal to a given number. It helps understand how values in a dataset accumulate over a range.

**Syntax:

ecdf( data_vector )

**Parameter:

1. Using Base R

To plot a CDF in R programming language, we first calculate it using the ecdf() function and then display it using the plot() function.

**Syntax:

plot(CDF)

**Parameter:

**Example 1: We plot the CDF of 500 random numbers

sample_Data <- rnorm(500) CDF <- ecdf(sample_Data) plot(CDF)

`

**Output:

gh

Cumulative Distribution Function in R

**Example 2: We plot the CDF of Petal.Length from the iris dataset

data(iris) plot(ecdf(iris$Petal.Length))

`

**Output:

gh

Cumulative Distribution Function in R

2. Plotting CDF of a Known Distribution

To plot the CDF of a standard distribution, we use the curve() function along with a probability function like pnorm.

**Syntax:

curve( expression, from, to )

**Parameters:

**Example: We plot the CDF of a normal distribution from -10 to 10.

curve(pnorm, from = -10, to = 10)

`

**Output:

gh

Cumulative Distribution Function in R

3. Plotting CDF Using ggplot2

We can also use the ggplot2 package to create styled CDF plots using stat_function().

**Syntax:

plot + stat_function( fun )

**Parameters:

**Example: We create a CDF plot for the normal distribution using ggplot2.

install.packages("ggplot2") library(ggplot2)

sample_limit <- data.frame(x = c(-10, 10))

ggplot(sample_limit, aes(x = x)) + stat_function(fun = pnorm, color = "skyblue", size = 2) + theme_minimal() + labs( title = "Cumulative Distribution Function (CDF) Plot", x = "X-axis", y = "Cumulative Probability", caption = "Created with ggplot2" )

`

**Output:

gh

Cumulative Distribution Function in R

This plot displays the cumulative distribution function of a normal distribution using ggplot2, with a styled curve, custom labels and a minimal visual theme.