R Quantile-Quantile (QQ) Plot – Base Graph

Sometimes it’s important to know whether the data is normally distributed. A quantile-quantile plot (QQ plot) is a good first check.

It shows the distribution of the data against the expected normal distribution.

typical quantile quantile qq plot

If the data is normally distributed, the points fall on the 45° reference line. If the data is non-normal, the points deviate noticeably from the reference line.

Create a Normal Quantile-Quantile (QQ) Plot

To get started, you need a set of data to work with.

Let’s consider the built-in ToothGrowth data set as an example data set.

Here are the first six observations of the data set.

Example: First six observations of the ‘ToothGrowth’ data set

Copied!> head(ToothGrowth)
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5

ToothGrowth data set

ToothGrowth data set contains observations on effect of vitamin C on tooth growth in 60 guinea pigs, where each animal received one of three dose levels of vitamin C (0.5, 1, and 2 mg/day) by one of two delivery methods, orange juice (coded as OJ) or ascorbic acid (coded as VC).

The qqnorm() function

In R, you can create the normal quantile-quantile plot using the qqnorm() function

This function plots your sample against a normal distribution.

You simply give the sample you want to plot as a first argument.

The qqline() function

R also has a qqline() function, which adds a theoretical distribution line to your normal QQ plot.

This line makes it a lot easier to evaluate whether the points deviate from the reference line.

The closer the points are to the reference line in the plot, the closer the sample data follows a normal distribution.

Example: Check ‘length’ sample in ‘ToothGrowth’ dataset for normality

> with(ToothGrowth, {
+   qqnorm(len);
+   qqline(len)})

Plot:

Notice that majority of the points are close to the reference line except in the extreme tail; so, you can assume normality.

Compare Two Samples

Use qqplot() function if you want to compare two samples (compare the quantiles of two samples).

This function sorts the data of both samples and plots them against each other.

For example, to check whether the tooth growth during supplement type ascorbic acid (coded as VC) and orange juice (coded as OJ) are distributed equally, you simply do the following.

Example:

> with(ToothGrowth,
+      qqplot(len[supp == "VC"],
+             len[supp == "OJ"]))

Plot:

QQ Plot with Confidence Intervals

The standard qqplot() functions in R do not provide confidence intervals but the qqPlot() function from the car package does.

Example: Create a QQ Plot with Confidence Intervals

> library(car)
> with(ToothGrowth, qqPlot(len))

Plot: