As you can see, wide_df has a column of :IDS which represents IDs and Subjects separated by an underline (_), e.g. ID001_S002 is ID 1 and Subject 2. Also, we have 4 columns which should be in a more tidy format:
Don’t worry about those columns, AoG.jl can handle them just fine. Speaking of AoG.jl, let’s load it with CairoMakie.jl as backend
usingCairoMakieusingAlgebraOfGraphics
1 🌌 The dims() arguments inside mapping() function
AoG.jl deals with wide data by passing the dims() function to keyword arguments inside the mapping() function. Inside the dims() you input an integer which represents which mapping()’s positional argument you’ll want to pivot. For example, dims(1) will pivot the first position argument inside mapping().
Let’s showcase dims() usage. As a start, we’ll create labels to hold our not so tidy columns:
We’ll pass a range of columns 2:6 which represents all the columns between the 2nd column (0.0) and the 6th column (4 hrs) as the first positional argument inside mapping().
Tip
Note that we need to vectorize the => operator since we are inputting a range/vector of columns. Thus we use the .=> vectorized pair syntax inside mapping().
Next, we pass the dims(1) to color keyword argument. This tells AoG.jl to use the first positional argument as the color mapping. We also continue the pair syntax inside mapping() with a renamer() and cleverly reusing our labels list of columns.
Note
Note that dims() will use the nth positional argument inside mapping(). For example:
dims(1) will use the first argument of mapping(:x, :y, :z), that is :x.
dims(2) will use the second argument of mapping(:x, :y, :z), that is :y.
dims(3) will use the third argument of mapping(:x, :y, :z), that is :z.
data(wide_df) *mapping(2:6.=>"Dosage") *mapping(; color =dims(1) =>renamer(labels) =>"Time") *AlgebraOfGraphics.density() |> draw
As you can see AoG.jl used the 5 columns as both the first positional argument and, thanks to dims(1), as the color keyword argument. No need to pivot!
Now, let’s show a more complex example of a bar plot using the same dims(1) also as the dodge keyword argument with a faceting by :IDS:
Now we can repeat the same code as before but facetting our rows by :IDS and our cols by :SUBJS. This is a much better visualization and showcase the full power of AoG.jl’s functionality:
data(split_df) *mapping(labels .=>"Dosage") *mapping(; color =dims(1) =>renamer(labels) =>"Time", dodge =dims(1) =>renamer(labels) =>"Time", row =:IDS, col =:SUBJS,) *visual(BarPlot) |> draw
This next plot compares the column 2 (0.0, the initial dosage) in the x-axis (as the first positional argument inside mapping()) with the other remaining columns 3:6 (the subsequent dosage measurements) in the y-axis (the second positional argument inside mapping()) by using a Scatter transformation inside visual() along with a linear() transformation:
There’s nothing special about dims(1): it just tells AoG.jl to use the first positional argument in mapping(). We can do an example where we use both dims(1) and dims(2) inside row and colmapping()’s keywords arguments, respectively.
The first positional argument is the first two columns of wide_df: 0.0 and 30 min. The second positional argument is the subsequent two columns: 1 hrs and 2 hrs.
Note that we are using the common 1-D array (vector) as the first positional argument, but we are using a row vector (without the ,) as the second positional argument.
This is because AoG.jl will combine them, and since we want the outer product of these vectors, we have to use one of them as a row vector. Also the elements inside the row vector are ordered different because of such operation.
2 🌔 facet arguments: link[x|y]axes
We can control how x- and y-axes behave while faceting. This is done with the facet keyword inside the draw() function.
To pass keyword arguments to customize facet’s attributes, you need to pass a NamedTuple of the desired keyword arguments to draw() via: