Skip to content

How to adjust the vertical position of geom_text when using position_dodge #1248

@syd-doyen

Description

@syd-doyen

Hi,

I'm just trying to understand whether the following behaviour is expected.
I'm using PyCharm-in windows 10, lets-plot 4.5.1

import pandas as pd
import os
import lets_plot as lp

df = pd.DataFrame(data=[['Subaru', 'Sold', 4], ['Subaru', 'InStock', 16],
                            ['Nissan', 'Sold', 2], ['Nissan', 'InStock', 8],
                            ['Ford', 'Sold', 3], ['Ford', 'InStock', 10],
                            ['Honda', 'Sold', 6], ['Honda', 'InStock', 2],
                            ], columns=['Brand', 'Status', 'Available'])
# the plot
p = (lp.ggplot(df, lp.aes(x='Brand', fill='Status', weight='Available'))
        + lp.geom_bar(position=lp.position_dodge(width=0.6), width=0.5, alpha=0.7)
        + lp.geom_text(lp.aes(label='..count..', group='Status'),
                         stat='count',
                         position=lp.position_dodge(width=0.6),
                         label_format="{d}",  size=9)
        + lp.ggtitle('Car Inventory') + lp.theme_bw())
p.show()

This code results in this:
image

This is fine but I want to slightly move the text higher so it is outside of the bar. Not sure how to accomplish this. I tried using nudge_y=0.5 (an argument in geom_text) with the following results.

p = (lp.ggplot(df, lp.aes(x='Brand', fill='Status', weight='Available'))
       + lp.geom_bar(position=lp.position_dodge(width=0.6), width=0.5, alpha=0.7)
       + lp.geom_text(lp.aes(label='..count..', group='Status'),
                         stat='count',
                         nudge_y = 0.5,
                         position=lp.position_dodge(width=0.6),
                         label_format="{d}",  size=9)
        + lp.ggtitle('Car Inventory') + lp.theme_bw())
p.show()

The vertical position is ok but the horizontal position has completely changed. It is not respecting the position_dodge.
See bellow. If this is expected any hints on how to move the geom_text to be higher than the bar?

image

Thanks in advance for your help.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions