Skip to content

Possible problem with geom_density() with after_stat('count') #550

@marcomoretto

Description

@marcomoretto

This code works as expected (density histogram with kde)

from plotnine import *
import pandas as pd

p_df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')

(
    ggplot(p_df.dropna(), aes(x='body_mass_g', y=after_stat('density')))
    + geom_density(aes(color='species'), alpha=0.8)
    + geom_histogram(aes(fill='species'), bins=20, alpha=0.5, position='identity')
)

image

But switching from after_stat('density') to after_stat('count') doesn't.

from plotnine import *
import pandas as pd

p_df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')

(
    ggplot(p_df.dropna(), aes(x='body_mass_g', y=after_stat('count')))
    + geom_density(aes(color='species'), alpha=0.8)
    + geom_histogram(aes(fill='species'), bins=20, alpha=0.5, position='identity')
)

image

The problem might be related to geom_density "ignoring" that count should be used instead of density. Same code as before with one histogram commented out.

from plotnine import *
import pandas as pd

p_df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')

(
    ggplot(p_df.dropna(), aes(x='body_mass_g', y=after_stat('count')))
    + geom_density(aes(color='species'), alpha=0.8)
    # + geom_histogram(aes(fill='species'), bins=20, alpha=0.5, position='identity')
)

image

The documentation state that it should work as expected

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions