Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate X/Y scales within facet layout #586

Merged
merged 17 commits into from
Jan 15, 2025

Conversation

jkrumbiegel
Copy link
Member

@jkrumbiegel jkrumbiegel commented Jan 15, 2025

This PR enables making plots like this where there are multiple different axes in a facet layout and they may be separate continuous and categorical ones. This works using the existing scale id feature, all the scales are kept separately and the plot is built up in one layer per scale id combination.

Before, AoG would throw an error if multiple different X or Y scales were detected in the figure. Separate axis labels within a facet plot were possible by using wide input data, but not categorical mixed with continuous as all columns in the wide input data have to belong to the same scale (at least currently).

This PR also adds and exports the zerolayer convenience function which can be useful for this scenario and fixes #437

dat = data((;
    fruit = rand(["Apple", "Orange", "Pear"], 150),
    taste = randn(150) .* repeat(1:3, inner = 50),
    weight = repeat(["Heavy", "Light", "Medium"], inner = 50),
    cost = randn(150) .+ repeat([10, 20, 30], inner = 50),
))

fruit = :fruit => "Fruit" => scale(:X1)
weights = :weight => "Weight" => scale(:Y1)
taste = :taste => "Taste Score" => scale(:X2)
cost = :cost => "Cost" => scale(:Y2)

layer1 = mapping(
    fruit,
    weights,
    col = direct("col1"), # this controls what facet this mapping belongs to
    row = direct("row1")
) * frequency()

layer2 = mapping(
    fruit,
    cost,
    col = direct("col1"),
    row = direct("row2")
) * visual(Violin)

layer3 = mapping(
    # note X and Y are flipped here for a horizontal violin
    weights,
    taste,
    col = direct("col2"),
    row = direct("row1")
) * visual(Violin, orientation = :horizontal)

layer4 = mapping(
    taste,
    cost,
    col = direct("col2"),
    row = direct("row2")
) * visual(Scatter)

spec = dat * (layer1 + layer2 + layer3 + layer4)

fg = draw(spec)
image

@jkrumbiegel jkrumbiegel merged commit a5e9aa5 into master Jan 15, 2025
5 checks passed
@jkrumbiegel jkrumbiegel deleted the jk/separate-scales-per-facet branch January 15, 2025 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Neutral additive element
1 participant