7.3 Statistical Visualizations
AlgebraOfGraphics.jl
can perform statistical transformations as layers with five functions:
expectation
: calculates the mean (expectation) of the underlying Y-axis columnfrequency
: computes the frequency (raw count) of the underlying X-axis columndensity
: computes the density (distribution) of the underlying X-axis columnlinear
: computes a linear trend relationship between the underlying X- and Y-axis columnssmooth
: computes a smooth relationship between the underlying X- and Y-axis columns
Let’s first cover expectation
:
plt = data(df) *
mapping(:name, :grade) *
expectation()
draw(plt)
Figure 57: AlgebraOfGraphics bar plot with expectation.
Here, expectation
adds a statistical transformation layer that tells AlgebraOfGraphics.jl
to compute the mean of the Y-axis values for every unique X-axis values. In our case, it computed the mean of grades for every student. Note that we could safely remove the visual transformation layer (visual(BarPlot)
) since it is the default visual transformation for expectation
.
Next, we’ll show an example with frequency
:
plt = data(df) *
mapping(:name) *
frequency()
draw(plt)
Figure 58: AlgebraOfGraphics bar plot with frequency.
Here we are passing just a single positional argument to mapping
since this is the underlying column that frequency
will use to calculate the raw count. Note that, as previously, we could also safely remove the visual transformation layer (visual(BarPlot)
) since it is the default visual transformation for frequency
.
Now, an example with density
:
plt = data(df) *
mapping(:grade) *
density()
draw(plt)
Figure 59: AlgebraOfGraphics bar plot with density estimation.
Analogous to the previous examples, density
does not need a visual transformation layer. Additionally, we only need to pass a single continuous variable as the only positional argument inside mapping
. density
will compute the distribution density of this variable which we can fuse all the layers together and visualize the plot with draw
.
For the last two statistical transformations, linear
and smooth
, they cannot be used with the *
operator. This is because *
fuses two or more layers into a single layer. AlgebraOfGraphics.jl
cannot represent these transformatinos with a single layer. Hence, we need to superimpose layers with the +
operator. First, let’s generate some data:
x = rand(1:5, 100)
y = x + rand(100) .* 2
synthetic_df = DataFrame(; x, y)
first(synthetic_df, 5)
x | y |
---|---|
1.0 | 1.96607080855035 |
5.0 | 6.993454790161161 |
5.0 | 5.709911916334441 |
3.0 | 4.544011304772464 |
4.0 | 5.382914898287261 |
Let’s begin with linear
:
plt = data(synthetic_df) *
mapping(:x, :y) *
(visual(Scatter) + linear())
draw(plt)
Figure 60: AlgebraOfGraphics scatter plot with linear trend estimation.
We are using the distribute property (Section 7) for more efficient code inside our mapping
, a * (b + c) = (a * b) + (a + b)
, where:
a
: thedata
andmapping
layers fused into a single layerb
: thevisual
transformation layerc
: the statisticallinear
transformation layer
linear
adds a linear trend between the X- and Y-axis mappings with a 95% confidence interval shaded region.
Finally, the same example as before but now replacing linear
with smooth
:
plt = data(synthetic_df) *
mapping(:x, :y) *
(visual(Scatter) + smooth())
draw(plt)
Figure 61: AlgebraOfGraphics scatter plot with smooth trend estimation.
smooth
adds a smooth trend between the X- and Y-axis mappings.
Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso