Skip to content

(dodged) Bars overlap axis when using 2xgeom_bar() and geom_text #798

@mcanouil

Description

@mcanouil

Take the code below:

from plotnine import *
import polars as pl

(
    ggplot(
        data=pl.DataFrame(
            {
                "X": ["x", "x", "y", "y"],
                "GROUP": ["A", "B", "A", "B"],
                "COUNT": [42, 24, 32, 18],
                "TOTAL": [74, 74, 50, 50],
            }
        )
    ) +
        aes(x="X", y="COUNT") +
        scale_y_continuous(expand=(0, 0, 0.05, 0)) +
        scale_x_discrete(expand=(0, 0, 0.05, 0)) +
        theme(axis_line=element_line()) +
        geom_bar(stat="identity") +
        geom_text(mapping=aes(label="TOTAL", y="TOTAL+1")) +
        geom_bar(mapping=aes(fill="GROUP"), position="dodge", stat="identity")
)

See how the dodged bars from the last geom overlaps both x/y-axis?

image

Now, if you remove one of the geom_ (any), there are no issues, e.g., removing geom_text:

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions