Produces a quantile-quantile (Q-Q) plot, also called a probability plot.
The qqPlot function is a modified version of the R functions
qqnorm and qqplot.
The EnvStats function qqPlot allows the user to specify a number of
different distributions in addition to the normal distribution, and to optionally
estimate the distribution parameters of the fitted distribution.
Usage
qqPlot(x, y = NULL, distribution = "norm", param.list = list(mean = 0, sd = 1),
estimate.params = plot.type == "Tukey Mean-Difference Q-Q",
est.arg.list = NULL, plot.type = "Q-Q", plot.pos.con = NULL, plot.it = TRUE,
equal.axes = qq.line.type == "0-1" || estimate.params, add.line = FALSE,
qq.line.type = "least squares", duplicate.points.method = "standard",
points.col = 1, line.col = 1, line.lwd = par("cex"), line.lty = 1,
digits = .Options$digits, ..., main = NULL, xlab = NULL, ylab = NULL,
xlim = NULL, ylim = NULL)Details
Extended methods and derivations are available in
Distribution diagnostics, transformations, and probability plots: qqPlot.
Open the vignette locally with
vignette("distribution-diagnostics", package = "EnvStats").
Arguments
- x
numeric vector of observations. When
yis not supplied,xrepresents a sample from the hypothesized distribution specifed bydistribution. Whenyis supplied, the distribution ofxis compared with the distribuiton ofy. Missing (NA), undefined (NaN), and infinite (Inf,-Inf) values are allowed but will be removed.- y
optional numeric vector of observations (not necessarily the same lenght as
x). Missing (NA), undefined (NaN), and infinite (Inf,-Inf) values are allowed but will be removed.- distribution
when
yis not supplied, a character string denoting the distribution abbreviation. The default value isdistribution="norm". See the help file forDistribution.dffor a list of possible distribution abbreviations. This argument is ignored ifyis supplied.- param.list
when
yis not supplied, a list with values for the parameters of the distribution. The default value isparam.list=list(mean=0, sd=1). See the help file forDistribution.dffor the names and possible values of the parameters associated with each distribution. This argument is ignored ifyis supplied orestimate.params=TRUE.- estimate.params
when
yis not supplied, a logical scalar indicating whether to compute quantiles based on estimating the distribution parameters (estimate.params=TRUE) or using the known distribution parameters specified inparam.list
(estimate.params=FALSE). The default value ofestimate.paramsisFALSEifplot.type="Q-Q"because the default configuration is a standard normal (mean=0, sd=1) Q-Q plot, which will yield roughly a straight line if the observations inxare from any normal distribution. The default value ofestimate.paramsisTRUEifplot.type="Tukey Mean-Difference Q-Q". The argumentestimate.paramsis ignored ifyis supplied.- est.arg.list
when
yis not supplied andestimate.params=TRUE, a list whose components are optional arguments associated with the function used to estimate the parameters of the assumed distribution (see the help file Estimating Distribution Parameters). For example, all functions used to estimate distribution parameters have an optional argument calledmethodthat specifies the method to use to estimate the parameters. (See the help file forDistribution.dffor a list of available estimation methods for each distribution.) To override the default estimation method, supply the argumentest.arg.listwith a component calledmethod; for exampleest.arg.list=list(method="mle"). The default value isest.arg.list=NULLso that all default values for the estimating function are used. This argument is ignored ifestimate.params=FALSEoryis supplied.- plot.type
a character string denoting the kind of plot. Possible values are
"Q-Q"(Quantile-Quantile plot, the default) and"Tukey Mean-Difference Q-Q"(Tukey mean-difference Q-Q plot). This argument may be abbreviated (e.g.,plot.type="T"to indicate a Tukey mean-difference Q-Q plot).- plot.pos.con
numeric scalar between 0 and 1 containing the value of the plotting position constant. The default value of
plot.pos.condepends on whether the argumentyis supplied, and if not the value of the argumentdistribution. Whenyis supplied, the default value isplot.pos.con=0.5, corresponding to Hazen plotting positions. Whenyis not supplied, for the normal, lognormal, three-parameter lognormal, zero-modified normal, and zero-modified lognormal distributions, the default value isplot.pos.con=0.375. For the Type I extreme value (Gumbel) distribution (distribution="evd"), the default value isplot.pos.con=0.44. For all other distributions, the default value isplot.pos.con=0.4.- plot.it
a logical scalar indicating whether to create a plot on the current graphics device. The default value is
plot.it=TRUE.- equal.axes
a logical scalar indicating whether to use the same range on the \(x\)- and \(y\)-axes when
plot.type="Q-Q". The default value isTRUEifqq.line.type="0-1"orestimate.params=TRUE, otherwise it isFALSE. This argument is ignored ifplot.type="Tukey Mean-Difference Q-Q".- add.line
a logical scalar indicating whether to add a line to the plot. If
add.line=TRUEandplot.type="Q-Q", a line determined by the value ofqq.line.typeis added to the plot. Ifadd.line=TRUEandplot.type="Tukey Mean-Difference Q-Q", a horizontal line at \(y=0\) is added to the plot. The default value isadd.line=FALSE.- qq.line.type
character string determining what kind of line to add to the Q-Q plot. Possible values are
"least squares"(the default),"0-1"and"robust". For the value"least squares", a least squares line is fit and added. For the value"0-1", a line with intercept 0 and slope 1 is added. For the value"robust", a line is fit through the first and third quartiles of thexandydata. This argument is ignored ifadd.line=FALSEorplot.type="Tukey Mean-Difference Q-Q".- duplicate.points.method
a character string denoting how to plot points with duplicate \((x,y)\) values. Possible values are
"standard"(the default),"jitter", and"number". For the value"standard", a single plotting symbol is plotted (this is the default behavior of R). For the value"jitter", a separate plotting symbol is plotted for each duplicate point, where the plotting symbols cluster around the true value of \(x\) and \(y\). For the value"number", a single number is plotted at \((x,y)\) that represents how many duplicate points are at that \((x,y)\) coordinate.- points.col
a numeric scalar or character string determining the color of the points in the plot. The default value is
points.col=1. See the entry forcolin the help file forparfor more information.- line.col
a numeric scalar or character string determining the color of the line in the plot. The default value is
points.col=1. See the entry forcolin the help file forparfor more information. This argument is ignored ifadd.line=FALSE.- line.lwd
a numeric scalar determining the width of the line in the plot. The default value is
line.lwd=par("cex"). See the entry forlwdin the help file forparfor more information. This argument is ignored ifadd.line=FALSE.- line.lty
a numeric scalar determining the line type of the line in the plot. The default value is
line.lty=1. See the entry forltyin the help file forparfor more information. This argument is ignored ifadd.line=FALSE.- digits
a scalar indicating how many significant digits to print for the distribution parameters. The default value is
digits=.Options$digits.- main, xlab, ylab, xlim, ylim, ...
additional graphical parameters (see
par).
Value
qqPlot returns a list with components x and y, giving the \((x,y)\)
coordinates of the points that have been or would have been plotted. There are four cases to
consider:
1. The argument y is not supplied and plot.type="Q-Q".
- x
the quantiles from the theoretical distribution.
- y
the observed quantiles (order statistics) based on the data in the argument
x.
2. The argument y is not supplied and plot.type="Tukey Mean-Difference Q-Q".
- x
the averages of the observed and theoretical quantiles.
- y
the differences between the observed quantiles (order statistics) and the theoretical quantiles.
3. The argument y is supplied and plot.type="Q-Q".
- x
the observed quantiles based on the data in the argument
x. Note that these are adjusted quantiles if the number of observations in the argumentxis greater then the number of observations in the argumenty.- y
the observed quantiles based on the data in the argument
y. Note that these are adjusted quantiles if the number of observations in the argumentyis greater then the number of observations in the argumentx.
4. The argument y is supplied and plot.type="Tukey Mean-Difference Q-Q".
- x
the averages of the quantiles based on the argument
xand the quantiles based on the argumenty.- y
the differences between the quantiles based on the argument
xand the quantiles based on the argumenty.
References
Chambers, J.M., W.S. Cleveland, B. Kleiner, and P.A. Tukey. (1983). Graphical Methods for Data Analysis. Duxbury Press, Boston, MA, pp.11-16.
Cleveland, W.S. (1993). Visualizing Data. Hobart Press, Summit, New Jersey, 360pp.
D'Agostino, R.B. (1986a). Graphical Analysis. In: D'Agostino, R.B., and M.A. Stephens, eds. Goodness-of Fit Techniques. Marcel Dekker, New York, Chapter 2, pp.7-62.
Author
Steven P. Millard (EnvStats@ProbStatInfo.com)
Note
A quantile-quantile (Q-Q) plot, also called a probability plot, is a plot of the observed order statistics from a random sample (the empirical quantiles) against their (estimated) mean or median values based on an assumed distribution, or against the empirical quantiles of another set of data (Wilk and Gnanadesikan, 1968). Q-Q plots are used to assess whether data come from a particular distribution, or whether two datasets have the same parent distribution. If the distributions have the same shape (but not necessarily the same location or scale parameters), then the plot will fall roughly on a straight line. If the distributions are exactly the same, then the plot will fall roughly on the straight line \(y=x\).
A Tukey mean-difference Q-Q plot, also called an m-d plot, is a modification of a Q-Q plot. Rather than plotting observed quantiles vs. theoretical quantiles or observed \(y\)-quantiles vs. observed \(x\)-quantiles, a Tukey mean-difference Q-Q plot plots the difference between the quantiles on the \(y\)-axis vs. the average of the quantiles on the \(x\)-axis (Cleveland, 1993, pp.22-23). If the two sets of quantiles come from the same parent distribution, then the points in this plot should fall roughly along the horizontal line \(y=0\). If one set of quantiles come from the same distribution with a shift in median, then the points in this plot should fall along a horizontal line above or below the line \(y=0\). A Tukey mean-difference Q-Q plot enhances our perception of how the points in the Q-Q plot deviate from a straight line, because it is easier to judge deviations from a horizontal line than from a line with a non-zero slope.
In a Q-Q plot, the extreme points have more variability than points toward the center. A U-shaped Q-Q plot indicates that the underlying distribution for the observations on the \(y\)-axis is skewed to the right relative to the underlying distribution for the observations on the \(x\)-axis. An upside-down-U-shaped Q-Q plot indicates the \(y\)-axis distribution is skewed left relative to the \(x\)-axis distribution. An S-shaped Q-Q plot indicates the \(y\)-axis distribution has shorter tails than the \(x\)-axis distribution. Conversely, a plot that is bent down on the left and bent up on the right indicates that the \(y\)-axis distribution has longer tails than the \(x\)-axis distribution.
Examples
# The guidance document USEPA (1994b, pp. 6.22--6.25)
# contains measures of 1,2,3,4-Tetrachlorobenzene (TcCB)
# concentrations (in parts per billion) from soil samples
# at a Reference area and a Cleanup area. These data are strored
# in the data frame EPA.94b.tccb.df.
#
# Create an Q-Q plot for the reference area data first assuming a
# normal distribution, then a lognormal distribution, then a
# gamma distribution.
# Assume a normal distribution
#-----------------------------
if (interactive()) dev.new()
with(EPA.94b.tccb.df, qqPlot(TcCB[Area == "Reference"]))
if (interactive()) dev.new()
with(EPA.94b.tccb.df, qqPlot(TcCB[Area == "Reference"], add.line = TRUE))
if (interactive()) dev.new()
with(EPA.94b.tccb.df, qqPlot(TcCB[Area == "Reference"],
plot.type = "Tukey", add.line = TRUE))
# The Q-Q plot based on assuming a normal distribution shows a U-shape,
# indicating the Reference area TcCB data are skewed to the right
# compared to a normal distribuiton.
# Assume a lognormal distribution
#--------------------------------
if (interactive()) dev.new()
with(EPA.94b.tccb.df,
qqPlot(TcCB[Area == "Reference"], dist = "lnorm",
digits = 2, points.col = "blue", add.line = TRUE))
if (interactive()) dev.new()
with(EPA.94b.tccb.df,
qqPlot(TcCB[Area == "Reference"], dist = "lnorm",
digits = 2, plot.type = "Tukey", points.col = "blue",
add.line = TRUE))
# Alternative parameterization
if (interactive()) dev.new()
with(EPA.94b.tccb.df,
qqPlot(TcCB[Area == "Reference"], dist = "lnormAlt",
estimate.params = TRUE, digits = 2, points.col = "blue",
add.line = TRUE))
if (interactive()) dev.new()
with(EPA.94b.tccb.df,
qqPlot(TcCB[Area == "Reference"], dist = "lnormAlt",
digits = 2, plot.type = "Tukey", points.col = "blue",
add.line = TRUE))
# The lognormal distribution appears to be an adequate fit.
# Now look at a Q-Q plot assuming a gamma distribution.
#----------------------------------------------------------
if (interactive()) dev.new()
with(EPA.94b.tccb.df,
qqPlot(TcCB[Area == "Reference"], dist = "gamma",
estimate.params = TRUE, digits = 2, points.col = "blue",
add.line = TRUE))
if (interactive()) dev.new()
with(EPA.94b.tccb.df,
qqPlot(TcCB[Area == "Reference"], dist = "gamma",
digits = 2, plot.type = "Tukey", points.col = "blue",
add.line = TRUE))
# Alternative Parameterization
if (interactive()) dev.new()
with(EPA.94b.tccb.df,
qqPlot(TcCB[Area == "Reference"], dist = "gammaAlt",
estimate.params = TRUE, digits = 2, points.col = "blue",
add.line = TRUE))
if (interactive()) dev.new()
with(EPA.94b.tccb.df,
qqPlot(TcCB[Area == "Reference"], dist = "gammaAlt",
digits = 2, plot.type = "Tukey", points.col = "blue",
add.line = TRUE))
#-------------------------------------------------------------------------------------
# Generate 20 observations from a gamma distribution with parameters
# shape=2 and scale=2, then create a normal (Gaussian) Q-Q plot for these data.
# (Note: the call to set.seed simply allows you to reproduce this example.)
set.seed(357)
dat <- rgamma(20, shape=2, scale=2)
if (interactive()) dev.new()
qqPlot(dat, add.line = TRUE)
# Now assume a gamma distribution and estimate the parameters
#------------------------------------------------------------
if (interactive()) dev.new()
qqPlot(dat, dist = "gamma", estimate.params = TRUE, add.line = TRUE)
# Clean up
#---------
rm(dat)
graphics.off()