How to use qqplot()?

The base R function ‘qqplot(x, y)’ takes two vectors of numeric values as arguments and plots their sorted values against each other. This allows us to visually compare if ‘x’ and ‘y’ come from similar distributions. For example, we can compare two random samples from different distributions:

R




set.seed(123)
x <- rnorm(100)
y <- rt(100, df = 5)
qqplot(x, y)
abline(0, 1)


Output:

qqplot in R

We can see that the points deviate from the reference line, especially at the tails, indicating that ‘x’ and ‘y’ have different distributions. However, if we want to check if a single variable (or residuals) follows a normal distribution, we need to compare it with a vector of quantiles from a normal distribution. This is what ‘qqPlot()’ from the ‘car` package does automatically. For example:

R




library(car)
x <- rnorm(100)
qqPlot(x)


Output:

qqPlot using car package in R

We can see that the points are close to the reference line and within the shaded area, indicating that ‘x’ follows a normal distribution. To achieve the same result with ‘qqplot()’, we need to manually generate a vector of quantiles from a normal distribution using ‘qnorm()‘ and ‘ppoints()’. For example:

R




# sample from normal distribution
x <- rnorm(100)
 
# vector of quantiles from standard normal distribution
z <- qnorm(ppoints(x))
qqplot(z, x)
 
# add reference line
abline(0, 1)


Output:

 

We can see that we get essentially the same plot as with `qqPlot()`, except for some minor differences in labels and aesthetics.

How to use qqplot() instead of qqPlot() in car package?

In this article, we will explain how to use the base R function ‘qqplot()’ instead of the ‘qqPlot()’ function from the `car` package to check the normality of a variable or a set of residuals. I will also show how to customize the plot and add confidence envelopes.

Similar Reads

What is a QQ plot?

A QQ plot (quantile-quantile plot) is a graphical tool that compares the empirical quantiles of a variable (or residuals) with the theoretical quantiles of a reference distribution (usually normal). The QQ plot can help us assess if the variable (or residuals) follows a certain distribution, by checking if the points fall approximately on a straight line. If there are deviations from linearity, such as curvature or outliers, it indicates that the variable (or residuals) does not follow that distribution....

How to use qqplot()?

The base R function ‘qqplot(x, y)’ takes two vectors of numeric values as arguments and plots their sorted values against each other. This allows us to visually compare if ‘x’ and ‘y’ come from similar distributions. For example, we can compare two random samples from different distributions:...

How to customize qqplot()?

...

How to add confidence envelopes?

...