Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
syd-doyen opened this issue Nov 18, 2024 · 1 comment
Closed
Assignees
Labels
Milestone

Comments

@syd-doyen
Copy link

syd-doyen commented Nov 18, 2024

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.

@alshan
Copy link
Collaborator

alshan commented Nov 19, 2024

Hi, it looks like nudge_y cancels position_dodge. Likely an issue but not 100% sure yet.
There are other ways however:

  1. Use vjust=0:
        + lp.geom_text(lp.aes(label='..count..', group='Status'),
                         stat='count',
                         vjust=0,
                         position=lp.position_dodge(width=0.6),
                         label_format="{d}",  size=9)

image

  1. Use vjust=0 and and geom_label with alpha:
    Unlike geom_text, geom_label has padding options.
        + lp.geom_label(lp.aes(label='..count..', group='Status'),
                         stat='count',
                         vjust=0, alpha=0, label_size=0,
                         position=lp.position_dodge(width=0.6),
                         label_format="{d}",  size=9)

image

  1. Use "layer labels" (a.k.a. annotations) instead of geoms.
    You might want to check out these docs: Annotating Charts.
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,
                      labels=lp.layer_labels().line('@..count..').format('..count..', '{d}')
                     )
        + lp.ggtitle('Car Inventory') + lp.theme_bw())

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants