7.3 Statistical Visualizations

AlgebraOfGraphics.jl can perform statistical transformations as layers with five functions:

Let’s first cover expectation:

plt = data(df) *
    mapping(:name, :grade) *
Figure 55: 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) *
Figure 56: 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) *
Figure 57: 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 transformations 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())
Figure 58: 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:

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())
Figure 59: 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