lets-plot icon indicating copy to clipboard operation
lets-plot copied to clipboard

geom_bar with stat='identity' does not stack values when only one group present

Open IKupriyanov-HORIS opened this issue 3 months ago • 4 comments

d = {
    'name': ['foo', 'foo', 'bar', 'bar'],
    'g': ['A', 'B', 'A', 'B'],
    'value': [80, 90, 15, 35]
}

ggplot(d) + geom_bar(aes(x='name', y='value', group='name'), stat='identity')

Actual result: Image

When values are assigned to their own group, everything works as expected (note the mapping group='g'):

Image

IKupriyanov-HORIS avatar Sep 19 '25 19:09 IKupriyanov-HORIS

The workaround (and IMO a proper way to build this bar plot) is to use count and weight aes:

d2 = {
    'name': ['foo', 'foo', 'bar', 'bar'],
    'value': [80, 90, 15, 35]
}

ggplot(d2) + geom_bar(aes(x='name', weight='value'), stat='count')

@IKupriyanov-HORIS , could it help to fix https://github.com/JetBrains/lets-plot-vega-lite/issues/2 ?

However, the following code should stack bars (currently it doesn't):

d3 = {
    'name': ['foo', 'foo', 'bar', 'bar'],
    'value': [80, 90, 15, 35]
}

ggplot(d3) + geom_bar(aes(x='name', y='value'), stat='identity')

This is an issue in 'stack' position adjustment I guess.

alshan avatar Sep 25 '25 18:09 alshan

The workaround (and IMO a proper way to build this bar plot) is to use count and weight aes: ... could it help to fix https://github.com/JetBrains/lets-plot-vega-lite/issues/2 ?

It could, but this will require some effort - the simplest obvious fix I made caused few Variable not found: '..count..' exceptions in tests.

IKupriyanov-HORIS avatar Sep 25 '25 20:09 IKupriyanov-HORIS

Variable not found: '..count..'

Impossible:) stat count (default) computes var ..count..

alshan avatar Sep 25 '25 20:09 alshan

However, the following code should stack bars (currently it doesn't):

d3 = { 'name': ['foo', 'foo', 'bar', 'bar'], 'value': [80, 90, 15, 35] }

ggplot(d3) + geom_bar(aes(x='name', y='value'), stat='identity') This is an issue in 'stack' position adjustment I guess.

I think, stacking require groups.

d = {
    'name': ['foo', 'foo', 'bar', 'bar'],
    'g': ['A', 'B', 'A', 'B'],
    'value': [80, 90, 15, 35]
}

ggplot(d) + geom_bar(aes(x='name', y='value', group='name'), stat='identity')

Here bars in each x-position are in the same group and will not stack.

However, position_stack has mode parameter which I don't fully understand :) , but which could help in one of these cases (or maybe both).

alshan avatar Oct 21 '25 16:10 alshan