VegaLite.jl icon indicating copy to clipboard operation
VegaLite.jl copied to clipboard

Use categorical value orders when plotting series data

Open east-winds opened this issue 4 years ago • 2 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)

east-winds avatar Sep 21 '20 17:09 east-winds

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

nivupaiR avatar Nov 27 '20 18:11 nivupaiR

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...

davidanthoff avatar Mar 11 '21 01:03 davidanthoff