has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
3.92k stars 210 forks source link

geom_text: can't use nudge_y and position=dodge #719

Closed ElPiloto closed 9 months ago

ElPiloto commented 9 months ago

The following snippet fails to alter the horizontal position. It seems the nudge_y prevents the position_dodge from working in the horizontal direction.

gg.geom_text(
      gg.aes(label='my_percent_metric*100.0', group=f'factor(some_factor)'),
      format_string='{:.2f}% ',
      nudge_y=0.00075,
      size=18,
      position=gg.position_dodge(width=0.9)

If I remove the nudge_y, then I see my text at the expected horizontal offsets.

Is this a bug or working as intended. If it's not a bug, could you elaborate on how I might achieve the desired functionality?

has2k1 commented 9 months ago

This is not a bug. "nudge_y = 0.00075" is equivalent to position = position_nudge(y=0.00075) So nudging overwrites position=position_dodge(...).

The solution is staged aesthetic evaluation.

aes(y=stage("coly", after_scale="y+0.00075"))

where "coly" is the original column that the y-aesthetic is mapped to. This modifies y after the any position adjustments have taken place.

At this point the nudge_y & nudge_x parameters are there for historical reasons. They predate the more powerful staged evaluation.

ElPiloto commented 9 months ago

Thanks for the response!