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

Last Updated : 29 Jul, 2025

The PDF is the acronym for Probability Distribution Function and CDF is the acronym for Cumulative Distribution Function. In general, there are many probability distribution functions in R programming Language.

1. PDF

The Probability Density Function (PDF) represents how probability is distributed for a continuous random variable.

**Syntax:

dnorm(x, mean, sd)

**Parameter:

2. CDF

The Cumulative Distribution Function (CDF) gives the probability that a variable takes a value less than or equal to a given number.

**Syntax:

ecdf(x)

**Parameter:

1. Plotting PDF Using plot Function

We generate a normal distribution using dnorm and then plot it using the base R plot function.

x <- seq(-15, 10) pdf <- dnorm(x, mean(x), sd(x)) plot(x, pdf, type="l", main="Normal Distribution PDF", xlab="x", ylab="Density")

`

**Output:

normal_distrubiton

Output

2. Plotting PDF Using plotpdf from gbutils Package

We use plotpdf from the gbutils package to plot the probability distribution curve with quantiles.

install.packages("gbutils") library(gbutils) x <- seq(-50, 10) pdf <- dnorm(x, mean(x), sd(x)) qdf <- function(x) qnorm(x, mean(x), sd(x)) plotpdf(pdf, qdf, lq = 0.0001, uq = 0.0009)

`

**Output:

graph

Output

3. Plotting CDF Using plot and ecdf

We compute the cumulative distribution using ecdf and visualize it using plot.

x <- seq(-15, 10) cdf <- ecdf(x) plot(cdf, main = "CDF Graph", xlab = "x", ylab = "Probability")

`

**Output:

scatter

Output

4. Plotting CDF Using plotpdf from gbutils Package

We define a custom cumulative distribution using pnorm and visualize it using plotpdf from the gbutils package.

install.packages("gbutils") library(gbutils) cdf1 <- function(x) pnorm(x, mean = -2.5, sd = 7.64) plotpdf(cdf1, cdf = cdf1, main = "CDF Plot")

`

**Output:

plot

Output