Draw a QuantileQuantile Plot in R Programming (original) (raw)

Draw a Quantile-Quantile Plot in R Programming

Last Updated : 6 Aug, 2025

A Quantile-Quantile plot is a graphical method for comparing two probability distributions by plotting their quantiles against each other. Typically, it is used to compare the distribution of the observed data with a theoretical distribution, such as the normal distribution.

When to Use Q-Q Plot in R

Q-Q plots are often used in statistical analysis to:

Implementation of Drawing Q-Q Plots in R

We are plotting Q-Q (Quantile-Quantile) plots to visually assess whether the sample data comes from a theoretical distribution like normal, exponential or t-distribution.

1. Installing and Loading Required Packages

We install the ggplot2 package and load it to allow advanced Q-Q plotting.

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

`

2. Drawing a Basic Q-Q Plot Using qqnorm

We are using base R's qqnorm function to create a basic Q-Q plot with a reference line.

data <- rnorm(100) qqnorm(data) qqline(data, col = "blue")

`

**Output:

Normal

Output

3. Drawing a Q-Q Plot Using ggplot2

We are creating a Q-Q plot with ggplot2 for better customization and visual clarity.

ggplot(data = data.frame(sample = data), aes(sample = sample)) + stat_qq() + stat_qq_line(col = "blue") + theme_minimal() + ggtitle("Q-Q Plot Using ggplot2")

`

**Output:

ggplot

Output

4. Creating a Q-Q Plot for Exponential Distribution

We are plotting a Q-Q plot to compare sample data with an exponential distribution.

exp_data <- rexp(100, rate = 1) qqplot(qexp(ppoints(100)), exp_data, main = "Q-Q Plot for Exponential Distribution") abline(0, 1, col = "blue")

`

**Output:

exponential_distribution

Output

5. Creating a Q-Q Plot for t-Distribution

We are plotting sample data against a t-distribution to check its conformity.

t_data <- rt(100, df = 5) qqplot(qt(ppoints(100), df = 5), t_data, main = "Q-Q Plot for t-Distribution") abline(0, 1, col = "red")

`

**Output:

t-distribution

Output

The output shows a Q-Q plot comparing sample data with a t-distribution, where most points lie along the red line, indicating the data approximately follows a t-distribution.