# Data Viz in Julia: Makie¶

I've tried Gadfly and enjoyed the grammar of graphics style of plotting, but I've heard lots of love for Makie as another library worth checking out. I'll use the CairoMakie backend for now, but it looks like the really fun stuff comes with the other backends like GLMakie in terms of interactivity and even making little interact apps.

# Makie¶

## First impressions¶

• The key concepts to think about are:
• Layouts and Figures
• Layoutables
• Plotting Functions
• The Figure object: Plots are created by initializing a Figure object (either explicitly or implicitly with a plotting function call like lines)
• Example: f = Figure(resolution = (600, 400))
• Like the size of the canvas, and you can specify how Layoutables like axes will go
• Layouts
• Makie supports some complex layouts - with your Figure object you can add subplots by adding axes
• Layoutables
• Axes and Legends are examples of Layoutables - what they call stuff that you can add to the Layout.
• Plotting Functions
• i.e. lines, scatter, series, etc.
• Calling a plotting function without referencing a figure will implicitly create a Figure
• What makes Makie so verbose is the fact that there are so many possible attributes that you can modify and set to customize the heck out of your chart.
• See plotting functions documentation for full list of attributes
• Observables
• Special workflow for interactive visualizations
• An Observable is an object that a Listener (usually a function) will react to whenever the Observable changes

Overall, the workflow feels like matplotlib or base plotly in that you're creating a canvas with your Figure and then adding components with Plotting Functions or stuff like Axes and Legends to that Figure incrementally. Each component has a bunch of attributes that you can fiddle around with and tweak to get the customized visualization that you want.

In [71]:
using CairoMakie
using DataFramesMeta
using AlgebraOfGraphics, PalmerPenguins

ENV["COLUMNS"] = 1000;

In [72]:
x = range(0, 10, length=100)
y = sin.(x)
lines(x, y)

Out[72]:

The lines call can be unpacked into a figure, axis, and pltobject separately if you want to attach attributes:

In [73]:
f, ax, pltobject = lines(x, y)
pltobject.attributes

Out[73]:
Attributes with 18 entries:
color => RGB{Float64}(0.0,0.447059,0.698039)
colormap => batlow
cycle => [:color]
depth_shift => 0.0
diffuse => Float32[0.4, 0.4, 0.4]
fxaa => false
inspectable => true
linestyle => nothing
linewidth => 1.5
model => Float32[1.0 0.0 0.0 0.0; 0.0 1.0 0.0 0.0; 0.0 0.0 1.0 0.0; 0.0 0.0 0.0 1.0]
nan_color => RGBA{Float32}(0.0,0.0,0.0,0.0)
overdraw => false
shininess => 32.0
specular => Float32[0.2, 0.2, 0.2]
ssao => false
transformation => Automatic()
transparency => false
visible => true

Mutating plot functions allow you to add to an existing figure:

In [84]:
x = range(0, 10, length=100)
y1 = sin.(x)
y2 = cos.(x)

scatter(x, y1, color = :red, markersize = 5, figure=(; resolution=(600, 400)))
scatter!(x, y2, color = :blue, markersize = 10)
current_figure()

Out[84]:

In fact, you can specify pretty much anything within the plot creation function:

In [79]:
lines(1:10, (1:10).^2; color=:black, linewidth=2, linestyle=:dash,
backgroundcolor=:grey90, fontsize=16),
axis=(; xlabel=L"x", ylabel=L"x^{2}", title="Chart Title",
xgridstyle=:dash, ygridstyle=:dash))
current_figure()

Out[79]:

Makie can take a matrix object as well, and there's a series plotting function that can do multiple lines at once:

In [94]:
dat2 = cumsum(randn(4, 101), dims = 2)

fig, ax, sp = series(dat2, labels=["label \$i" for i in 1:4], figure=(; resolution=(640,400)))
axislegend(ax)
current_figure()

Out[94]:

We can see that Makie works just fine for vectors and matrices, but what about DataFrames? Well, each column of a dataframe is a vector so you could get by with loops, but we have to write a lot of extra throwaway code which can get annoying fast.

# AlgebraOfGraphics¶

While Makie is powerful, it does seem a little verbose if you work with tabular data in DataFrames. It's recommended if you want to build complex or 'publication quality' plots or you want interactivity, but it could use a wrapper to make things faster for one-off plots.

ggplot2 is still the go-to plotting library for R, and using it within the tidyverse feels like a well thought out, integrated system that is both easy to use and easy to layer complexity on top of it.

Gadfly follows very closely in the ggplot2 Grammar of Graphics style, so it already feels familiar (dare I say pleasing) to use with DataFramesMeta and comes with plenty of customization right out of the bat.

What makes the GoG-style plotting libraries feel so powerful is that you can add or remove layers of complexity on top of the plot without having to rewrite a lot of code. It's flexible in that the core building blocks work together seamlessly once you learn the grammar.

Enter AlgebraOfGraphics. AlgebraOfGraphics uses Makie in the background so that you still have all of the options with layouts and attributes, but it allows you to write similarly expressive statements that give you some of the benefits of GoG. However, it's just different enough that it takes some getting used to.

We can start with an example and you can see the code in action:

In [8]:
# on the first run, if it asks for stdin type in 'y' and hit enter to download
first(penguins, 6)

Out[8]:

6 rows × 7 columns (omitted printing of 1 columns)

speciesislandbill_length_mmbill_depth_mmflipper_length_mmbody_mass_g
String15String15Float64Float64Int64Int64
In [9]:
set_aog_theme!() # just like seaborn's sns.set()

axis = (width = 225, height = 225)
penguin_frequency = data(penguins) * frequency() * mapping(:species)

draw(penguin_frequency; axis)

Out[9]:

The line:

penguin_frequency = data(penguins) * frequency() * mapping(:species)


is an example of the Algebra of Layers, where Layers are the key building block or abstraction. Each layer is a product of a combination of elementary objects:

• Data
• Mapping: associate data to plot attributes
• Visual: plotting functions from Makie
• Analyses: i.e. Histogram, frequency table

And finally, once you've specified the Layers you use the draw function to render it:

draw(penguin_frequency) # renders the Layers


Let's revisit our first lines plot from Makie using this AoG language:

In [10]:
df = DataFrame(x = range(0, 10, length=100), y = sin.(x))
plt = data(df) * mapping(:x, :y)
draw(plt * visual(Lines); axis)

Out[10]:

The multiplication operator (*) is being used to add objects within the same layer, while the addition operator (+) is used to combine more than one Layer into a list of Layers.

The draw function operates on either Layer or Layers and assigns it to the specified figure or axis

In [23]:
df = DataFrame(dat, :auto)
x2 = data(df) * mapping(:x1, :x2) * visual(Lines, color="orange")
x3 = data(df) * mapping(:x1, :x3) * visual(Lines, color="green", linewidth=3)
# Use the addition operator to combine our two Layer(s)
draw(x2 + x3; axis)

Out[23]:

It's still a little early for AlgebraOfGraphics but it's nice to see the GoG style on top of the power of Makie. I'd still prefer to use Makie itself or Gadfly for one-off plots - and knowing a lower level library is always useful.

In [ ]: