queryverse / VegaLite.jl

Julia bindings to Vega-Lite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use categorical value orders when plotting series data

east-winds opened this issue · comments

The Plots package uses the ordering of categorical values when plotting series data, changing the order of assigning colors and in the legend. Currently, VegaLite does not respect the ordering (and appears to default to alphabetical).

MWE:

using Plots, VegaLite, DataFrames

# Sample dataframe with categorical variable
df = DataFrame(name = repeat(["a","b","c"], outer = [3]),
                x = repeat([1, 2, 3], inner = [3]),
                y = rand(9))
df.name = categorical(df.name)
levels(df.name)

# Plot with the original ordering (a,b,c)
df |>
@vlplot(:line, 
    x=:x, y=:y, 
    color={"name:n"})

# Change the categorical ordering and check
levels!(df.name, reverse(levels(df.name)))
levels(df.name)

# Plot with the new ordering (unchanged) -- should be (c,b,a)
df |>
@vlplot(:line, 
    x=:x, y=:y, 
    color={"name:n"})

# For reference, Plots.plot uses the new correct ordering (c,b,a)
plot(df.x, df.y, group = df.name)

Do we know if this feature (sorting the plots in arbitrary/based on values/non alphabetical order) support is being added to VegaLite?

Yeah, I've been thinking how we could do that... I don't really want to take a dependency on CategoricalArrays.jl, that is quite an involved package. I wish there was some lightweight interface that would allow VegaLite.jl to learn about an ordering without taking a dependency on a concrete implementation of a categorical story...