# Diagramming + Data Visualization with Julia

## A new approach to Data Visualization using Vizagrams.jl

Data visualization and diagramming are usually treated as distinct subjects. Yet, for the keen observer, they are quite similar. In fact, one could say that data visualization is a subset of diagramming, where data is used in a structured manner to generate a diagram drawing. This idea is explored in the Julia package Vizagrams.jl, which implements a diagramming Domain Specific Language (DSL) with a data visualization grammar built on top of it.

In this article, we show how to use **Vizagrams.jl** both for diagramming and data visualization. I highly recommend using a notebook such as Jupyter or Pluto in order to follow along.

# Installation

The package is registered in the Julia general repository, hence, it can be installed by simply doing:

`julia>]`

pkg> add Vizagrams

# The Basics of Diagramming

Vizagrams implements a diagramming DSL inspired in the great Haskell library Diagrams. We can think of a diagram as simply a collection of primitive graphical objects (circles, rectangles, lines…), where the final drawing simply renders each object one after the other, much like an SVG.

We start with the most basic example, a single circle:

`using Vizagrams`

# My first Diagram

d = Circle()

draw(d)

By default, our circle is drawn as black. We can modify its color by applying a style transformation.

`d = S(:fill=>:red)*Circle()`

draw(d)

We can make things more interesting by adding another object to our drawing. How can we do this? Well, just add it:

`d = S(:fill=>:red)*Circle() + Square()`

draw(d)

Note that the order of the summation matters. By adding a square after the circle, Vizagrams renders the square above the circle.

We have used the `S(:fill=>:red)`

to apply the fill color red to the circle. Besides stylistic transformations, we can also apply geometric transformations, such as translation `T`

, rotation `R`

and scaling `U`

.

`d = S(:fill=>:red)*Circle() +`

S(:stroke=>:blue,:fill=>:white)T(2,0)*Square() +

S(:fill=>:white)R(3.14/4)*Square()

draw(d)

Finally, we can combine also combine existing diagrams to form new ones.

`d_2 = d + T(4,0) * d`

draw(d_2)

# Constructing Data Visualizations

Let us now go to plotting. Again, we start with the most basic example:

`plt = plot(x=rand(100),y=rand(100),color=rand(["a","b"],100))`

draw(plt)

As expected, the result is a simple scatter plot. Yet, there is something interesting going on. The variable `plt`

is actually holding a diagram. Which means that we can manipulate it just like we did previously with other diagrams, and we can also combine it with other diagrams.

`d = R(π/4)plt +`

T(250,0)*plt +

Line([[180,250],[350,350],[350,200]]) +

T(350,350)Circle(r=10)

draw(d)

Although the example above was silly, it does illustrates the possibilities of what can be done. In fact, it is easy to see how one can combine diagramming operations to construct more useful visualizations, such as:

# Visualization Grammars with Diagrams

In Vizagrams, the diagramming DSL was used to build a data visualization grammar, i.e. a specification that can produce a variety of visualizations. The syntax for the grammar is based on Vega-Lite, which is a very popular grammar in the Data Visualization community (the Python package Altair is based on Vega-Lite, and there is also a Julia package called VegaLite.jl).

Explaining visualization grammars would take an article on its own. Yet, the specification style if fairly simple, so hopefully even those not familiar with Vega-Lite will understand what is going on.

`# Importing DataFrames to store the data`

using DataFrames

# VegaDatasets is used to load the dataset `cars`

using VegaDatasets

df = DataFrame(dataset("cars"));

df = dropmissing(df);

# Here is where Vizagrams plot specification actually starts

plt = Plot(

data=df,

encodings=(

x=(field=:Horsepower,),

y=(field=:Miles_per_Gallon,),

color=(field=:Origin,),

size=(field=:Acceleration,),

),

graphic=Circle()

)

draw(plt)

Note that we specified our plot by first passing on the dataset. Then, we defined the “encodings”, which were `x`

, `y`

, `color`

and `size`

. Each encoding variable has a parameter `field`

which says which column in the dataset is mapped to it. Thus, in our example, we are mapping “Horsepower” to the x-axis, “Miles_per_Gallon” to the y-axis, the color is varying according to the “Origin” and the size according to “Acceleration”. At last, the “graphic” is specifying what is to be drawn in this plot. Since we passed `Circle()`

, this means that we are drawing circles.

Here is where things get interesting. We can pass diagrams to this “graphic” parameter in the plot specification. First, let us create a diagram:

`d = S(:fill=>:white,:stroke=>:black)*Circle(r=2) +`

Circle() +

T(2,0)*Square()

draw(d)

Now, we place this diagram inside the plot specification, and…

`plt = Plot(`

data=df,

encodings=(

x=(field=:Horsepower,),

y=(field=:Miles_per_Gallon,),

color=(field=:Origin,),

size=(field=:Acceleration,),

),

graphic = Mark(d)

)

draw(plt)

Again, this example is not very useful, yet, it illustrates the sort of things that can be achieved. Here is perhaps a more “useful” example:

# Some Final Words

This article was a mere introduction to Vizagrams. There is much more to be explored, such as graphical marks creation, graphic expressions, stacking operations, and so on.

If you are interested in learning more about Vizagrams, check out the documentation. Again, I recommend using a notebook (Jupyter or Pluto), as one can quickly experiment with different designs.