## Basic visualization in Julia – Gadfly

In this post we will walk through basics of Gadfly – visualization package written in Julia. Gadfly is Julia implementation of layered grammar of graphics proposed by Hadley Wickham who implemented his idea into ggplot2 package being the main visualization library in R. One spicy note, the original inventor of “grammar of graphics” (the one who was inspiration for Wickham) is now hired by Tableau Software – leading company in data visualization.

The main motivation for grammar of graphics is to formalize visualization for statistics. Authors use word “grammar” so one can think of set of rules that let you build “correct” (with respect to given grammar) sentences. In this case though sentence is graphical so one can see the output in a form of a plot.

Lets now try to provide declarative description of what a plot is and then use this knowledge to actually plot stuff.

Plot consists of:

• Aesthetics – it can be understood as plot interface for data. Data is binded to aesthetics. Different aesthetics are expected for different kinds of plots. For example to plot set of points one can use geometry Geom.point (don’t worry yet – geometry is explained in a minute) that requires aesthetics x and y . In other words these aesthetics are always known at the time of plot creation. Knowing what you want to plot there is always specification of what aethetics chosen geometry requires – so it is not an art but rather a craft to choose proper aesthetics.
• Geometries – geometry is what defines what will be plotted, what is the geometry of your data. Each geometry requires set of aesthetics to work. Please take a look at specification of Geom.point – it requires aethetics x and y as was noted above. Different kind of geometries define different plots. The geometry is then a central point of your plot – geometries and aesthetics define what you want to plot while other components specify how you want to do it.
• Statistics – It is a middle layer between aesthetics provided and geometry. So whenever you provide aesthetics for given geometry there is corresponding statistics in the middle – very often that statistics is simply “identity” (like in case of Geom.point)
• Scales – to transform axes of your plot, (to land with log-scale of x for scatterplot one can use Scale.x_log10)
• Guides – elements responsible for plotting axis labels, titles etc.

Ok, having that basic knowledge lets try to play with Gadfly a bit. Installation of the package is as easy as:

Pkg.add("Gadfly")


Its recommended to install Cairo that serves as PDF/PS/PNG backend (in case you want to export your plots)

Pkg.add("Cairo")


We will need a dataset to play with. Its probably good time to mention Rdatasets package that gives Julia programmers access to R datasets

Lets quickly install and load it:

Pkg.add("RDatasets")
using RDatasets


For the pursposes of this post we will work with sleepstudy dataset from package “lme4”. We will be looking at certain tasks reaction times of people who were sleeping less then recommended 9 days in a row (details of dataset here)

sleep = dataset("lme4","sleepstudy")


Lets first list columns of sleep DataFrame:

names(sleep)
3-element Array{Symbol,1}:
:Reaction
:Days
:Subject


Reaction is a reaction time in milliseconds, Days represents day number of sleep deprivation and Subject is the subject id.

Ok lets first take a look at scatterplot of day number and reaction time with no subject distinction. To plot scatterplot we need to use Geom.point geometry and attach proper columns to x and y

plot(sleep, x = "Days", y = "Reaction", Geom.point) We can append another layer to plot function, Geom.smooth – to fit smooth curve to dataset provided, it does not require additional aesthetics.

plot(sleep, x = "Days", y = "Reaction", Geom.point, Geom.smooth) Not perfect yet, we still would like to have x scale to be discrete, one can use Scale.x_discrete to force it. Moreover lets point out Reaction time is measured in ms and set a title for our plot. Guide will help us here.

plot(sleep, x = "Days", y = "Reaction", Geom.point, Geom.smooth, Scale.x_discrete,
Guide.ylabel("Reaction - ms"), Guide.title("Reaction time across days of sleep deprivation")) Now lets say we would like to investigate general reaction ability for every subject. Lets say we want a density plot that shows reaction time

plot(sleep, x = "Reaction", Geom.density, color = "Subject", Scale.x_continuous(minvalue= 0, maxvalue= 500)) This shows individual variances of reaction time. You can note that Subject 309 is probably a machine or at least immortal human.

How these values look across differet days

plot(sleep, x = "Days", y ="Reaction", Geom.point, Geom.smooth, color = "Subject", Scale.y_continuous(minvalue = 200, maxvalue = 500)) (btw if you are reading it late at night it is probably good reason to go to sleep)

All plots above required dataframe as input. It is not mandatory of course. One can provide data vector directly to aesthetics

plot(x = sleep[:Reaction], Geom.histogram(bincount = 30), Scale.x_continuous(minvalue = 200), color = sleep[:Days]) Another convenient way is to stack many layers on one plot. Lets say we want to compare reaction time of subject 309 and 310 on one plot

plot(
layer(x = sleep[:Days][sleep[:Subject] .== "309"], y = sleep[:Reaction][sleep[:Subject] .== "309"], Geom.point, Geom.smooth, Theme(default_color=color("red"))),
layer(x = sleep[:Days][sleep[:Subject] .== "310"], y = sleep[:Reaction][sleep[:Subject] .== "310"], Geom.point, Geom.smooth, Theme(default_color=color("blue")))
,Guide.XLabel("Days"), Guide.YLabel("Reaction time - ms")) Please note Theme argument provided to each layer, using Theme you can set plot parameters such as: default_color, point size, font size of x label, font size of y label and many more. More about themes here

As you can see Gadfly gives you quite flexible environment when it comes to visualization. Lets try to summarize plotting in Julia with Gadfly. It is not painful at all, very friendly for R users. It gives flexibility to the users, after some time of playing with it I find it more and more convenient

If you want to can check out some more advanced examples of Gadfly plotting take a look here. Grammar of Graphics – which I would guess is a classic is available on Amazon

---
{"ft_lang_label":"__label__en","ft_lang_prob":0.88058203,"math_prob":0.95091337,"size":6280,"snap":"2020-10-2020-16","text_gpt3_token_len":1411,"char_repetition_ratio":0.11902486,"word_repetition_ratio":0.013875124,"special_character_ratio":0.22882165,"punctuation_ratio":0.12687813,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96782166,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],"im_url_duplicate_count":[null,5,null,5,null,5,null,5,null,5,null,5,null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-24T09:06:27Z\",\"WARC-Record-ID\":\"<urn:uuid:e65d3836-2b18-4cbe-8d9e-350d4d0d013e>\",\"Content-Length\":\"35861\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f8549834-0e44-4585-8cc2-30eac68f0a8c>\",\"WARC-Concurrent-To\":\"<urn:uuid:ccb5ec80-c1b0-4815-8e92-486a2ae0c33b>\",\"WARC-IP-Address\":\"104.27.148.187\",\"WARC-Target-URI\":\"https://int8.io/basic-visualization-in-julia-gadfly/\",\"WARC-Payload-Digest\":\"sha1:7JGTBQXS56DR555KTGTXCKSF5XKDSKLH\",\"WARC-Block-Digest\":\"sha1:MPT2JS7EXP2KHRW6OV7VY73ZQ2YJ2HLP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875145910.53_warc_CC-MAIN-20200224071540-20200224101540-00246.warc.gz\"}"}