Draw a Quantile-Quantile Plot in detail
A Quantile-Quantile (Q-Q) plot is a graphical tool used to compare two probability distributions by plotting their quantiles against each other. Often, it is employed to compare the distribution of observed data with a theoretical distribution (for example, the normal distribution).
When to Use Q-Q Plots in R
Q-Q plots are useful in statistical analysis to:
- Assess Normality: Check if a dataset follows a normal distribution, which is a common assumption in many tests.
- Detect Skewness or Kurtosis: Identify whether data have heavy tails or skewed shapes.
- Compare Distributions: Evaluate if two datasets originate from the same distribution.
Setting Up R for Q-Q Plotting
Before creating Q-Q plots, ensure that the necessary packages are installed and loaded. While base R provides built-in Q-Q plotting functions, the ggplot2 package allows for enhanced customization.
# Install and load ggplot2 if needed
install.packages("ggplot2")
library(ggplot2)
1. Creating a Basic Q-Q Plot Using Base R
The qqnorm() function in base R is a straightforward method to produce a Q-Q plot against the normal distribution.
Example
# Generate sample data from a normal distribution (150 values)
normal_data <- rnorm(150, mean = 0, sd = 1)
# Create a Q-Q plot using base R
qqnorm(normal_data, main = "Normal Q-Q Plot (Base R)")
qqline(normal_data, col = "blue") # Adds a reference line
Output:

2. Creating a Basic Q-Q Plot Using ggplot2
For a more refined and customizable plot, ggplot2 offers an excellent alternative.
Example
# Create a data frame containing the sample data
df <- data.frame(value = normal_data)
# Generate a Q-Q plot using ggplot2
ggplot(df, aes(sample = value)) +
stat_qq(color = "darkgreen") +
stat_qq_line(color = "blue") +
theme_classic() +
ggtitle("Normal Q-Q Plot using ggplot2")
Output:

3. Q-Q Plots for Other Distributions
A. Exponential Distribution
To compare your data with an exponential distribution, you can generate the theoretical quantiles using qexp() and then create a Q-Q plot with qqplot().
Example
# Generate sample data from an exponential distribution (120 values, rate = 1)
exp_data <- rexp(120, rate = 1)
# Create a Q-Q plot comparing the exponential sample to theoretical quantiles
qqplot(qexp(ppoints(120), rate = 1), exp_data,
main = "Exponential Q-Q Plot",
xlab = "Theoretical Quantiles",
ylab = "Sample Quantiles")
abline(0, 1, col = "blue") # Reference line for comparison
Output:

B. t-Distribution
Similarly, to check if data follow a t-distribution, use the qt() function for generating theoretical quantiles.
Example
# Generate sample data from a t-distribution (150 values, df = 7)
t_data <- rt(150, df = 7)
# Create a Q-Q plot comparing the t-distributed sample to theoretical quantiles
qqplot(qt(ppoints(150), df = 7), t_data,
main = "t-Distribution Q-Q Plot",
xlab = "Theoretical Quantiles",
ylab = "Sample Quantiles")
abline(0, 1, col = "red") # Adds the equality line
Output:
























