Efficient visualizations with layouts

Introduction to Data Visualization with Julia

Gustavo Vieira Suñe

Data Analyst

Layouts

  • Multiple curves in one figure

A density plot displaying the distribution of ages grouped by the frequency of K-Pop listening, with one curve per category shown in the same figure.

  • Plot grid (layout)

A two-by-two grid of density plots displaying the distribution of ages one plot for each frequency of K-Pop listening. The line of each plot has a different color.

Introduction to Data Visualization with Julia

The grid

using StatsPlots, DataFrames, CSV

# Load dataset
streaming = DataFrame(
    CSV.File("streaming.csv")
)

# Create density plot density( streaming.Age, group=streaming."Frequency [K pop]", linewidth=2.5,
# Set layout layout=4, )

A two-by-two grid of density plots displaying the distribution of ages one plot for each frequency of K-Pop listening. All plots have the same line color.

Introduction to Data Visualization with Julia

Customizing grid elements

using Colors

# Julia logo colors
logocolors = Colors.JULIA_LOGO_COLORS
colors = [logocolors.blue logocolors.red
    logocolors.green logocolors.purple]

density( streaming.Age, group=streaming."Frequency [K pop]", linewidth=2.5, layout=4, # Line colors linecolor=colors, )

A two-by-two grid of density plots displaying the distribution of ages one plot for each frequency of K-Pop listening. The line of each plot has a different color.

Introduction to Data Visualization with Julia

Controlling the grid layout

density(
    streaming.Age,
    group=streaming."Frequency [K pop]",
    linewidth=2.5,
    linecolor=colors,

# Layout dimensions layout=(4, 1),
# Axis labels xlabel=["" "" "" "Age"], ylabel="Probability", )
# Axis bounds xlims!(10, 80) ylims!(0, 0.2)

A four-by-one grid of density plots displaying the distribution of ages one plot for each frequency of K-Pop listening. The line of each plot has a different color.

Introduction to Data Visualization with Julia

Advanced layouts

A grid of plots. The top row displays two box plots, one of ages and the other of anxiety levels versus streaming service. The bottom row has three histograms displaying the distribution of ages for each streaming service.

Introduction to Data Visualization with Julia

Step-by-step

theme(:wong)
# Choose colors
colors = [:purple :green3 :firebrick1]


# First box plot p1 = boxplot(streaming."Streaming service", streaming.Age,
# Group by streaming service group=streaming."Streaming service", color=colors,
label=false, ylabel="Age", # Remove outliers outliers=false)

A box plot displaying the distribution of ages per streaming service.

Introduction to Data Visualization with Julia

Step-by-step

# Second box plot
p2 = boxplot(
    streaming."Streaming service",
    streaming.Anxiety,
    # Group by streaming service
    group=streaming."Streaming service",
    color=colors,
    label=false,
    ylabel="Anxiety",
    # Remove outliers
    outliers=false,
)

A box plot displaying the distribution of anxiety levels per streaming service.

Introduction to Data Visualization with Julia

Step-by-step

# Histograms
p3 = histogram(
    streaming.Age,
    group=streaming."Streaming service",
    color=colors,
    # Remove lines
    linewidth=0,

# Set layout layout=(1, 3),
xlabel="Age", # Set y-axis labels ylabel=["Frequency" "" ""] )

Three histograms displaying the distribution of ages for each streaming service.

Introduction to Data Visualization with Julia

Joining the plots

# Select layout
layout = @layout [a b; c]

A figure displaying the layout structure of the plot.

# Join the plots
plot(p1, p2, p3, layout=layout)

A grid of plots. The top row displays two box plots, one of ages and the other of anxiety levels versus streaming service. The bottom row has three histograms displaying the distribution of ages for each streaming service.

Introduction to Data Visualization with Julia

Let's practice!

Introduction to Data Visualization with Julia

Preparing Video For Download...