Creates a univariate graphical summary combining a histogram (or probability mass plot for discrete data), a kernel density curve, a boxplot, and an empirical cumulative distribution function in a single multi-panel figure. Optional components include a rug, and fitted theoretical distribution curves for both the histogram and the ECDF panel.
Usage
plotFreqDist(
x,
main = NULL,
xlab = "",
xlim = NULL,
heights = NULL,
hist = TRUE,
dens = TRUE,
rug = FALSE,
curve = FALSE,
boxplot = TRUE,
ecdf = TRUE,
curveEcdf = FALSE,
stamp = .useTheme,
...
)Arguments
- x
numeric vector whose distribution is to be plotted.
- main
main title.
NULL(default) draws none, unless the theme setsautoTitle = TRUE(see theme); the title is then derived from the expression passed asx."",NAorFALSEalways suppress it and compact the top margin; any other string is used as given.- xlab
label for the x-axis. The variable name is typically placed in
main, so this defaults to"".- xlim
range of the x-axis.
NULL(default) uses the range of the histogram breaks, extended by 5% on both sides.- heights
numeric vector of relative panel heights for
layout(): three values for histogram/boxplot/ecdf, two for histogram/boxplot or histogram/ecdf only.NULL(default) chooses automatically.- hist
controls the histogram panel.
TRUE(default) uses package defaults;FALSE/NAsuppresses the panel (xlimthen falls back to the range of the data, extended by 5%); a list overrides specific arguments forwarded tographics::hist(). The elementtypeselects the plot style:"hist"(standard histogram, chosen automatically for continuous or high-cardinality data) or"mass"(vertical bars per unique value, for discrete/low-cardinality data). The elementpanel.lastadds to the histogram after it is drawn: a function without arguments or a quoted call, e.g.list(panel.last = quote(abline(v = 0, lty = 2))).- dens
controls the kernel density curve.
TRUE(default) draws a curve viastats::density(); a list overrides specific arguments (e.g.list(bw = 0.1, col = "red")). The extra elementboundsgives the support ofxasc(lower, upper), defaultc(-Inf, Inf). A finite bound is handled by reflection: the data are mirrored at the bound, the density is estimated with the bandwidth of the original data and folded back. The curve then neither leaks past the bound nor drops towards it. Use e.g.list(bounds = c(0, Inf))for non-negative data orc(0, 1)for proportions.- rug
controls a rug plot.
FALSE(default) suppresses it;TRUEor a list draw a rug viagraphics::rug().- curve
controls a fitted theoretical distribution curve on the histogram.
FALSE(default),NULL, orNAsuppress it;TRUEdraws a normal curve withmean(x)/sd(x); a list may includeexpras a function ofxor a one-sided formula inxfor any distribution (e.g.list(expr = ~ dt(x, df = 2), col = "darkgreen")). The formula is evaluated in the environment it was written in, so local variables may be referenced (see the gamma example below).- boxplot
controls the boxplot panel.
TRUE(default) draws a horizontal boxplot with a mean marker and CI band;FALSE/NAsuppresses the panel. A list overrides arguments forwarded tographics::boxplot(); two extra elements control the mean display:pch.mean(default3) andcol.meanci(defaultgetTheme()$grid$col). Set either toNAto suppress that element.- ecdf
controls the ECDF panel.
TRUE(default) callsplotECDF(); a list overrides specific arguments.- curveEcdf
controls a fitted theoretical CDF curve on the ECDF panel, analogous to
curve.FALSE(default) suppresses it.- stamp
controls the corner stamp.
.useTheme(default) resolves togetTheme()$stamp.TRUE/FALSE/NULL, a string, or a named list forstamp().- ...
further graphical parameters passed to
par()via the internal framework. Note thatmargiven here sets the outer margins (oma) of the multi-panel figure; the inner panel margins are managed internally and cannot be overridden.
Details
Each plot component is controlled via a single argument accepting
bedrock::callIf() semantics:
TRUE: draw with package defaultsFALSE,NULL, orNA: suppress entirelya named list: draw with the given overrides merged into the defaults
Performance: for very large vectors (n > 1e7) the density curve, ECDF,
and semi-transparent boxplot outliers will still take noticeable time.
For exploratory work on very large data, consider sampling first:
plotFreqDist(x[sample(length(x), 5000)]).
See also
hist(), boxplot(), plotECDF(),
density(), rug(), layout(),
theme
Other plot.univariate:
plotArea(),
plotBar(),
plotBox(),
plotCatDist(),
plotDens(),
plotDensBox(),
plotDot(),
plotECDF(),
plotLines(),
plotQQ(),
plotRidge(),
plotViolin()
Examples
plotFreqDist(faithful$eruptions)
# custom histogram breaks, density color, boxplot styling
plotFreqDist(faithful$eruptions,
hist = list(breaks = 50),
dens = list(col = "olivedrab4"),
boxplot = list(col = "olivedrab2", pch.mean = NA, col.meanci = NA))
# no density, no ecdf, add rug instead
plotFreqDist(faithful$eruptions,
dens = FALSE, ecdf = FALSE,
hist = list(xaxt = "s"),
rug = TRUE,
heights = c(3, 2.5),
main = "Eruption time")
# non-negative data: reflect the density at 0 instead of letting it
# leak into negative values
set.seed(1)
plotFreqDist(rexp(500), dens = list(bounds = c(0, Inf)))
# the same for real data: tips cannot be negative
plotFreqDist(bedrock::Pizza$tip, dens = list(bounds = c(0, Inf)),
main = "Pizza - tip")
# overlay a normal density curve
x <- rnorm(1000)
plotFreqDist(x, curve = TRUE, boxplot = FALSE, ecdf = FALSE)
# compare with a t-distribution curve
plotFreqDist(x,
curve = list(expr = ~ dt(x, df = 2), col = "darkgreen"),
boxplot = FALSE, ecdf = FALSE)
# overlay gamma distribution on both histogram and ECDF
ozone <- airquality$Ozone
m <- mean(ozone, na.rm = TRUE)
v <- var(ozone, na.rm = TRUE)
plotFreqDist(ozone,
hist = list(breaks = 15),
curve = list(expr = ~ dgamma(x, shape = m^2/v, scale = v/m),
col = "navajowhite3"),
curveEcdf = list(expr = ~ pgamma(x, shape = m^2/v, scale = v/m),
col = "navajowhite3"),
main = "Airquality - Ozone")
