R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
A Quantile-Quantile (Q-Q) plot is a graphical tool to help assess if a dataset follows a particular theoretical distribution. It plots the quantiles of the dataset against the quantiles of the chosen theoretical distribution. A 45-degree reference line is often included: if the data follows the chosen distribution, the points should fall close to this line.
The most common Q-Q plot is the Normal Q-Q plot, which checks if the data follows a normal distribution. However, Q-Q plots can be made for any distribution by adjusting the quantiles accordingly.
Here's a tutorial on how to draw a Normal Q-Q plot in R:
For demonstration purposes, we'll generate some normally distributed sample data:
set.seed(123) data <- rnorm(100)
You can use the qqnorm()
function to plot the sample quantiles against the theoretical quantiles of the normal distribution:
qqnorm(data, main="Normal Q-Q Plot", xlab="Theoretical Quantiles", ylab="Sample Quantiles") # Adding the reference line qqline(data, col="red", lwd=2)
qqline()
adds a reference line which passes through the first and third quartiles of the data.
ggplot2
Package:If you prefer ggplot2
for plotting, you can make a Q-Q plot as follows:
First, install and load the ggplot2
package:
install.packages("ggplot2") library(ggplot2)
Now, draw the Q-Q plot:
ggplot(data.frame(data=data), aes(sample=data)) + stat_qq(distribution=qt, dparams=list(df=mean(data))) + stat_qq_line(distribution=qt, dparams=list(df=mean(data)), col="red", lwd=2) + ggtitle("Normal Q-Q Plot") + xlab("Theoretical Quantiles") + ylab("Sample Quantiles")
Q-Q plots are a valuable diagnostic tool to visually assess the distributional assumptions of your data. They can be easily produced in R using either base R functions or ggplot2
, depending on your preference.
Quantile-Quantile plot in R:
# Generate a random sample from a normal distribution data <- rnorm(100) # Create a Q-Q plot qqplot(data, main="Q-Q Plot")
Creating Q-Q plots in R:
qqplot()
function.# Generate two random samples sample1 <- rnorm(100) sample2 <- rexp(100) # Create Q-Q plot comparing the two samples qqplot(sample1, sample2, main="Q-Q Plot")
qqnorm and qqline functions in R:
qqnorm()
and qqline()
functions in R for creating Q-Q plots with added normality reference line.# Generate a random sample data <- rnorm(100) # Create Q-Q plot with a reference line qqnorm(data, main="Q-Q Plot") qqline(data)
Normal quantile-quantile plot in R:
# Generate a random sample from a normal distribution data <- rnorm(100) # Create Q-Q plot for normality check qqnorm(data, main="Normal Q-Q Plot") qqline(data)
Customizing Q-Q plots in R:
# Generate a random sample data <- rnorm(100) # Customized Q-Q plot qqnorm(data, main="Custom Q-Q Plot", col="blue", pch=19) qqline(data, col="red")
Comparing distributions with Q-Q plots in R:
# Generate two random samples sample1 <- rnorm(100) sample2 <- rt(100, df=3) # Compare distributions using Q-Q plot qqplot(sample1, sample2, main="Q-Q Plot for Distribution Comparison")
R ggplot2 Q-Q plot example:
# Install and load ggplot2 package install.packages("ggplot2") library(ggplot2) # Generate a random sample data <- rnorm(100) # Create Q-Q plot with ggplot2 ggplot(data.frame(x=data), aes(sample=x)) + stat_qq() + ggtitle("ggplot2 Q-Q Plot")