gg_qq_plot: Q-Q Plot using ggplot2

View source: R/gg_qq_plot.R

gg_qq_plotR Documentation

Q-Q Plot using ggplot2

Description

This is a version of the quantile-quantile (Q-Q) plot using ggplot2. In addition to the Q-Q plot, marginal distributions are also provided.

Usage

gg_qq_plot(
  samp,
  theoretical_dist = "norm",
  bins = 20,
  probs = c(0.25, 0.75),
  loess = FALSE,
  span = 1,
  title,
  xlab = "Theoretical Quantiles",
  ylab = "Sample Quantiles",
  theme,
  ...
)

Arguments

samp

a numeric vector of the sample.

theoretical_dist

the distribution function to use to draw a theoretical distribution. This can be any function that follows R's conventions for distributions. That is, there needs to be a rXXX and qXXX version where XXX is the name of the distribution function (e.g. norm, t, chisq).

bins

the number of bins to use for the histograms.

probs

numeric vector of length two, representing probabilities. Corresponding quantile pairs that define the line drawn.

loess

whether to include a Loess regression line.

span

if loess = TRUE, the alpha parameter that controls the amount of smoothing.

title

plot title.

xlab

label for the x-axis.

ylab

label for the y-axis.

theme

a ggplot2 theme (optional).

...

other parameters passed to the theoretical_dist function.

Value

a Q-Q plot.

Examples

pop_size <- 100000
samp_size <- 50

distributions <- data.frame(
	unif_pop = runif(pop_size),
	skew_pop = rchisq(pop_size, df = 5),
	norm_pop = rnorm(pop_size, mean = 2, sd = 1)
)

# Draw random samples from each of our populations.
unif_samp <- sample(distributions$unif_pop, size = samp_size)
skew_pop <- sample(distributions$skew_pop, size = samp_size)
norm_pop <- sample(distributions$norm_pop, size = samp_size)

# Q-Q plots for our three samples
gg_qq_plot(unif_samp, title = 'Normal Quantile-Quantile Plot (Uniform Population)')
gg_qq_plot(skew_pop, title = 'Normal Quantile-Quantile Plot (Skewed Population)')
gg_qq_plot(norm_pop, title = 'Normal Quantile-Quantile Plot (Normal Population)')

jbryer/VisualStats documentation built on Feb. 27, 2025, 6:19 p.m.