# How to Create & Interpret a Q-Q Plot in R

Quantile-Quantile (Q-Q) plots are a very powerful tool for the identification of data distribution. They allow us to graphically analyze and compare our data with a theoretical distribution such as normal or exponential. When there’s a match between our data and the theoretical distribution, the points in the Q-Q plot will approximately lie on a straight line.

The Q-Q plot is used primarily to check for normality in the data, but it can be used for any distribution if you know the distribution your data should theoretically follow. If the data points lie on a line in the Q-Q plot, then your data is distributed as per your theoretical distribution.

This article will explain how to create and interpret a Q-Q plot in R. We’ll walk through the whole process. By the end of this article, you should be comfortable with using Q-Q plots in R to identify data distributions.

## Generating Q-Q Plots

Let’s see how to generate a Q-Q plot.

We’ll use a built-in dataset in R, named mtcars. The dataset is a collection of various car attributes, and we’ll focus on the mpg (miles per gallon) column. The mpg column consists of continuous data, which can be analyzed using a Q-Q plot.

The general process is to compare the quantiles of our sample data with the quantiles of a theoretical distribution. If the sample comes from the same distribution, the points should fall approximately along a straight line.

The command to generate a Q-Q plot in R is qqnorm(). However, we are going to use ggplot2 because of its flexibility and versatility. The basic code to create a Q-Q plot using ggplot2 in R is as follows:

# Create a dataframe with the standard normal theoretical quantiles and sample quantiles
df <- data.frame(theoretical_quantiles = qnorm(ppoints(mtcars$mpg)), sample_quantiles = sort(mtcars$mpg))

# Generate the Q-Q plot
library(ggplot2)

ggplot(df, aes(x = theoretical_quantiles, y = sample_quantiles)) +
geom_point() +
geom_line(aes(y = theoretical_quantiles), colour = "red") +
ggtitle("Q-Q plot of mtcars mpg") +
xlab("Theoretical Quantiles") +
ylab("Sample Quantiles")

This script first creates a new data frame df which contains the theoretical quantiles of a standard normal distribution and the sorted sample quantiles of the mpg column from the mtcars dataset. The ppoints() function is used to generate probability points for the standard normal theoretical quantiles.

The ggplot() function is then used to create the Q-Q plot. The aes() function is used to map the theoretical quantiles to the x-axis and the sample quantiles to the y-axis.

The geom_point() function is used to add points to the plot for each observation in the data.

The geom_line() function is used to add a reference line to the plot. The reference line represents where the points would lie if the sample data followed a standard normal distribution perfectly.

The ggtitle(), xlab(), and ylab() functions are used to add a title to the plot and labels to the x-axis and y-axis, respectively.

## Interpreting the Q-Q Plot

Now that we have our Q-Q plot, let’s see how to interpret it.

If the sample data follow the standard normal distribution, the points in the Q-Q plot will approximately lie along the red line (theoretical quantiles line). The closer the points are to the line, the closer the data is to a normal distribution.

If the points in the Q-Q plot are not close to the line, or if they follow some other pattern, this suggests that the data does not follow a normal distribution. For example:

• If the points in the Q-Q plot lie below the line on one end and above the line on the other end, this suggests that the data may be skewed.
• If the points in the Q-Q plot curve upwards away from the line, this suggests that the data may have heavy tails (i.e., there are more extreme values than would be expected in a normal distribution).
• If the points in the Q-Q plot curve downwards away from the line, this suggests that the data may have light tails (i.e., there are fewer extreme values than would be expected in a normal distribution).

Remember, the Q-Q plot gives you a visual tool to inspect your data distribution and identify deviations from the theoretical distribution. But it does not provide a formal hypothesis test for normality. If you want a more formal statistical test for normality, you should consider the Shapiro-Wilk test, Anderson-Darling test, or similar.

## Conclusion

In conclusion, the Q-Q plot is a very useful tool for visually analyzing and identifying the distribution of your data. It provides a means of comparing the quantiles of your sample data to the quantiles of a standard normal distribution (or any other theoretical distribution). The closer the points in the Q-Q plot are to the reference line, the closer the data is to the theoretical distribution. The Q-Q plot can be easily generated in R using the ggplot2 package.

Posted in RTagged