Advanced Calculations

Calculate Percentage of Total

using VegaLite, DataFrames

data = DataFrame(
    Activity=["Sleeping","Eating","TV","Work","Exercise"],
    Time=[8,2,4,8,2]
)

data |>
@vlplot(
    height={step=12},
    :bar,
    transform=[
        {
            window=[{op="sum",field="Time",as="TotalTime"}],
            frame=[nothing,nothing]
        },
        {
            calculate="datum.Time/datum.TotalTime * 100",
            as="PercentOfTotal"
        }
    ],
    x={"PercentOfTotal:q", axis={title="% of total Time"}},
    y={"Activity:n"}
)

A bar graph showing what activites consume what percentage of the day.

Calculate Difference from Average

using VegaLite, VegaDatasets

dataset("movies") |>
@vlplot(
    transform=[
        {filter="datum.IMDB_Rating != null"},
        {
            joinaggregate= [{
                op=:mean,
                field=:IMDB_Rating,
                as="AverageRating"
            }]
        },
        {filter="(datum.IMDB_Rating - datum.AverageRating) > 2.5"}
    ]
) +
@vlplot(
    :bar,
    x={"IMDB_Rating:q",axis={title="IMDB Rating"}},
    y={"Title:o"}
) +
@vlplot(
    mark={
        :rule,
        color="red"
    },
    x={"AverageRating:q", aggregate="average"}
)