Quantile – Quantile plot in R or QQ Plot in R

Quantile – Quantile plot in R which is also known as QQ plot in R is one of the best way to test how well the data is distributed normally. QQ plot is even better than histogram to test the normality of the data. we will be plotting Q-Q plot with qqnorm() function in R. Q-Q plot in R is explained with example.

For what QQ plot is used for ?

  • QQ plot is used to test the normality of a data
  • QQ plot is used to compare two data

Let’s see both with an example

 

Quantile – Quantile plot in R to test the normality of a data:

In R, qqnorm() function plots your data against a standard normal distribution.

  1. Give data as an input to qqnorm() function
  2. R takes up this data and create a sample values with standard normal distribution
  3. Then R compares these two data sets (input data set and generated standard normal data set)
  4. Sorts both the data sets
  5. Then finally plots these two sorted data sets against each other.

All the above steps are done simply by using QQnorm function in R

 

Quantile – Quantile plot in R Example (test the normality):

Let consider inbuilt “trees” data set and let’s check the normality of trees height

# QQ plot in R to test the normality of data

qqnorm(trees$Height,main="Height of black cherry trees")
qqline(trees$Height) ## adds the line to the plot

when we give trees height as an input to the qqnorm() function in R. R executes all the above mentioned steps and returns the following QQ plot

quantile quantile plot in r qq plot in R Quantile - Quantile plot in R example

Quantile – Quantile plot in R to compare two data set:

  1. In this method R simply takes up two data sets
  2. Sorts both the data sets
  3. Plots these two sorted data sets against each other.

Example of QQ plot in R (compare two data set):

Lets use same trees data set and compare the trees Girth and its Volume with QQ plot function as shown below

# QQ plot in R to compare two data samples

qqplot(trees$Volume,trees$Girth, main="Volume vs Girth of trees")

two data (volume and girth) are sorted and plotted against each other, so the output will be

quantile quantile plot in r qq plot in R 1 Quantile - Quantile plot in R example

previous small qq plot in r Quantile - Quantile plot in R example                                                                                                                next small qq plot in R Quantile - Quantile plot in R example