Analyses
Histogram
AlgebraOfGraphics.histogram
— Functionhistogram(; bins=automatic, datalimits=automatic, closed=:left, normalization=:none)
Compute a histogram.
The attribute bins
can be an Integer
, an AbstractVector
(in particular, a range), or a Tuple
of either integers or abstract vectors (useful for 2- or 3-dimensional histograms). When bins
is an Integer
, it denotes the approximate number of equal-width intervals used to compute the histogram. In that case, the range covered by the intervals is defined by datalimits
(defaults to the extrema of the data). When bins
is an AbstractVector
, it denotes the intervals directly.
closed
determines whether the the intervals are closed to the left or to the right.
The histogram can be normalized by setting normalization
. Possible values are:
:pdf
: Normalize by sum of weights and bin sizes. Resulting histogram has norm 1 and represents a PDF.:density
: Normalize by bin sizes only. Resulting histogram represents count density of input and does not have norm 1.:probability
: Normalize by sum of weights only. Resulting histogram represents the fraction of probability mass for each bin and does not have norm 1.:none
: Do not normalize.
Weighted data is supported via the keyword weights
.
Normalizations are computed withing groups. For example, in the case of normalization=:pdf
, sum of weights within each group will be equal to 1
.
using AlgebraOfGraphics, CairoMakie
set_aog_theme!()
df = (x=randn(5000), y=randn(5000), z=rand(["a", "b", "c"], 5000))
specs = data(df) * mapping(:x, layout=:z) * histogram(bins=range(-2, 2, length=15))
draw(specs)
specs = data(df) * mapping(:x, dodge=:z, color=:z) * histogram(bins=range(-2, 2, length=15))
draw(specs)
specs = data(df) * mapping(:x, stack=:z, color=:z) * histogram(bins=range(-2, 2, length=15))
draw(specs)
specs = data(df) *
mapping((:x, :z) => ((x, z) -> x + 5 * (z == "b")) => "new x", col=:z) *
histogram(datalimits=extrema, bins=20)
draw(specs, facet=(linkxaxes=:minimal,))
data(df) * mapping(:x, :y, layout=:z) * histogram(bins=15) |> draw
Density
AlgebraOfGraphics.density
— Functiondensity(; datalimits=automatic, kernel=automatic, bandwidth=automatic, npoints=200)
Fit a kernel density estimation of data
. Here, datalimits
specifies the range for which the density should be calculated, and kernel
and bandwidth
are forwarded to KernelDensity.kde
. npoints
is the number of points used by Makie to draw the line
df = (x=randn(5000), y=randn(5000), z=rand(["a", "b", "c", "d"], 5000))
specs = data(df) * mapping(:x, layout=:z) * AlgebraOfGraphics.density(datalimits=((-2.5, 2.5),))
draw(specs)
specs = data(df) *
mapping((:x, :z) => ((x, z) -> x + 5 * (z ∈ ["b", "d"])) => "new x", layout=:z) *
AlgebraOfGraphics.density(datalimits=extrema)
draw(specs, facet=(linkxaxes=:minimal,))
data(df) * mapping(:x, :y, layout=:z) * AlgebraOfGraphics.density(npoints=50) |> draw
specs = data(df) * mapping(:x, :y, layout=:z) *
AlgebraOfGraphics.density(npoints=50) * visual(Surface)
draw(specs, axis=(type=Axis3, zticks=0:0.1:0.2, limits=(nothing, nothing, (0, 0.2))))
Frequency
AlgebraOfGraphics.frequency
— Functionfrequency()
Compute a frequency table of the arguments.
df = (x=rand(["a", "b", "c"], 100), y=rand(["a", "b", "c"], 100), z=rand(["a", "b", "c"], 100))
specs = data(df) * mapping(:x, layout=:z) * frequency()
draw(specs)
specs = data(df) * mapping(:x, layout=:z, color=:y, stack=:y) * frequency()
draw(specs)
specs = data(df) * mapping(:x, :y, layout=:z) * frequency()
draw(specs)
Expectation
AlgebraOfGraphics.expectation
— Functionexpectation()
Compute the expected value of the last argument conditioned on the preceding ones.
df = (x=rand(["a", "b", "c"], 100), y=rand(["a", "b", "c"], 100), z=rand(100), c=rand(["a", "b", "c"], 100))
specs = data(df) * mapping(:x, :z, layout=:c) * expectation()
draw(specs)
specs = data(df) * mapping(:x, :z, layout=:c, color=:y, dodge=:y) * expectation()
draw(specs)
specs = data(df) * mapping(:x, :y, :z, layout=:c) * expectation()
draw(specs)
Linear
AlgebraOfGraphics.linear
— Functionlinear(; interval=automatic, level=0.95, dropcollinear=false, npoints=200)
Compute a linear fit of y ~ 1 + x
. An optional named mapping weights
determines the weights. Use interval
to specify what type of interval the shaded band should represent, for a given coverage level
(the default 0.95
equates alpha = 0.05
). Valid values of interval
are :confidence
, to delimit the uncertainty of the predicted relationship, and :prediction
, to delimit estimated bounds for new data points. Use interval = nothing
to only compute the line fit, without any uncertainty estimate. By default, this analysis errors on singular (collinear) data. To avoid that, it is possible to set dropcollinear=true
. npoints
is the number of points used by Makie to draw the shaded band.
x = 1:0.05:10
a = rand(1:7, length(x))
y = 1.2 .* x .+ a .+ 0.5 .* randn.()
df = (; x, y, a)
specs = data(df) * mapping(:x, :y, color=:a => nonnumeric) * (linear() + visual(Scatter))
draw(specs)
Smoothing
AlgebraOfGraphics.smooth
— Functionsmooth(; span=0.75, degree=2, npoints=200)
Fit a loess model. span
is the degree of smoothing, typically in [0,1]
. Smaller values result in smaller local context in fitting. degree
is the polynomial degree used in the loess model. npoints
is the number of points used by Makie to draw the line
x = 1:0.05:10
a = rand(1:7, length(x))
y = sin.(x) .+ a .+ 0.1 .* randn.()
df = (; x, y, a)
specs = data(df) * mapping(:x, :y, color=:a => nonnumeric) * (smooth() + visual(Scatter))
draw(specs)
This page was generated using Literate.jl.