TidierOrg / TidierPlots.jl

Tidier data visualization in Julia, modeled after the ggplot2 R package.
MIT License
196 stars 7 forks source link

`geom_line` with colors connects different traces #93

Closed jfb-h closed 2 months ago

jfb-h commented 2 months ago

If you plot different traces, the start of each trace gets connected to the next. Here's a reproducer:

df = mapreduce(vcat, 1:4) do i 
    DataFrame(
        x=collect(1:100),
        y=cumsum(randn(100)),
        g=fill(string(i), 100)
    )
end

ggplot(df, aes(x=:x, y=:y, color=:g)) + geom_line()

grafik

rdboyes commented 2 months ago

Thanks for reporting this - it's another problem (similar to https://github.com/TidierOrg/TidierPlots.jl/issues/86) that is downstream from the "ad hoc" way I've handled the idea of "grouping" so far in the package. Hoping to have a fix for this on the way to 0.8.0

adknudson commented 2 months ago

I have a MWE of grouping that may be useful. I'm not sure how it's being handled in TidierPlots (somewhere in draw.jl?).

using DataFrames, ColorSchemes, CairoMakie

import Makie.SpecAPI as S

df = DataFrame(
    a = rand(20),
    b = rand(20),
    c = rand(["L", "H"], 20),
    d = rand(["A", "B"], 20),
)

group_df = groupby(df, [:c, :d])

palette = ColorSchemes.tab10

ax = S.Axis()

for (i, key) in enumerate(keys(group_df))
    s = S.Lines(group_df[key].a, group_df[key].b, color = palette[i])
    push!(ax, s)
end

layout_spec = S.GridLayout(ax)
plot(layout_spec)

It seems like the way to do grouping in Makie is to make multiple plot specs.

rdboyes commented 2 months ago

Thanks @adknudson - this was essentially how I was imagining this would be fixed, but helpful to have a MWE. I added support for geoms that return multiple PlotSpecs when I was fixing geom_smooth (just need to have them return a Vector{PlotSpec} and they should draw automatically). All we need to do is have geoms conditionally return a vector of plotspecs when appropriate and this should work

rdboyes commented 2 months ago

This is working for geom_path in a dev branch:

image

geom_line isn't currently cooperating - the sort is conflicting with the grouping. Should be fixed soon!

rdboyes commented 2 months ago

geom_line fixed - will merge soon