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

Feature Request: Permanent Tooltip / Annotate last datapoint #1244

Open
nsiicm0 opened this issue Nov 14, 2024 · 4 comments
Open

Feature Request: Permanent Tooltip / Annotate last datapoint #1244

nsiicm0 opened this issue Nov 14, 2024 · 4 comments
Labels
* enhancement New feature or request
Milestone

Comments

@nsiicm0
Copy link

nsiicm0 commented Nov 14, 2024

Hi all
when exporting plots as static assets (such as PNG), tooltips are obviously no longer working. So sometimes it would be handy to automatically (based on provided mappings) annotate/label certain data points permanently. geom_text / geom_label would be potential options, however, this gets awfully complicated if you have for instance a line plot with various lines (just as an example).
I found this R ggplot package that somewhat overcomes this and somewhat does what I envision: https://cmap-repos.github.io/cmapplot/reference/geom_text_lastonly.html however, this does not completely solve all the issues.

Since we are already able to specify tooltips and format them to our preferences, wouldn't it be neat if we could have the already defined tooltip be permanently visible (without hovering) for a given x-axis datapoint (or automagically for "the first"/"the last" datapoint) if we either say so explicitely or call ".to_png()"?

@syd-doyen
Copy link

Hi,

I do something similar to what you request by filtering on the geom_text .

I'm sharing an example just where the last point of each group is labelled with its value in case you find it useful.

# example for showing how to annotate the last point
# data frame to illustrate the example
df2 = pd.DataFrame(data=[
                        ['2024Q1', 'Sold', 4], ['2024Q1', 'InStock', 16], ['2024Q1', 'Arriving', 5],
                        ['2024Q2', 'Sold', 8], ['2024Q2', 'InStock', 16], ['2024Q2', 'Arriving', 7],
                        ['2024Q3', 'Sold', 12], ['2024Q3', 'InStock', 16], ['2024Q3', 'Arriving', 25]
                        ], columns=['Quarter', 'Status', 'Units'])

# this is the quarter that will be annotated
last_quarter = '2024Q3'
p3 = (lp.ggplot(df2, lp.aes(x='Quarter', y='Units', colour='Status'))
         + lp.geom_point(shape=1, size=4)
         + lp.geom_line(size=1.3)
         + lp.geom_text(
                data=df2[df2['Quarter'] == last_quarter],
                mapping=lp.aes(label='Units', y='Units'), size=8, nudge_x=0.1, label_format="{d}", show_legend=False)
         + lp.theme_bw())

image

Cheers,

@alshan
Copy link
Collaborator

alshan commented Nov 19, 2024

Hi guys,

I found this R ggplot package that somewhat overcomes this and somewhat does what I envision: https://cmap-repos.github.io/cmapplot/reference/geom_text_lastonly.html however, this does not completely solve all the issues.

Geom "last only" is a surprising idea)

We are planning to address this general topic with the help of layer annotation settings (see Annotating Charts). Layer annotations currently present to some extent in geom_pie and geom_bar.
Maybe geom_line is the next. Would be great to hear about your needs in this area.

@nsiicm0
Copy link
Author

nsiicm0 commented Nov 20, 2024

Hi all
thanks for your feedback.

@syd-doyen 's answer is a good approach - I have not thought of using geom_text like this. My current (hacky) solution for line plots is to use geom_segment and geom_label and I calculate the y-axis coordinate based on the data by hand in order to draw something like this.
image
The problem that I am facing with line plots is: The plots can be of varying length (time duration on the x-axis) and the x-axis might be either a string representation of a data or a date object data type. So any manual placing is quite fiddly.

For bar plots I do the same and annotate on the highest y-axis coordinate. So @alshan 's solution with the labels is actually something I have not thought of either and might be helpful here in the future. With a surrogate field that is empty if not on the last x-axis datapoint, I can achieve what I basically want on my bar plots using the labels:
image
(obviously format needs to be adjusted for a real world use case - but this seems a doable method)

The use case for me is definitely the annotation of the last element on the x-axis (x-axis = time component). For whatever reason, the people that I am creating these plots for have grown accustomed to having their plots annotated in specific places (usually the last data point on the x-axis) to quickly glance the most recent value. My plots usually make quite extensive use of the tooltips, hence, my initial suggestion to basically have a trigger that can be triggered to permanently show the tooltip of a given datapoint (i.e. when preparing a plot to be exported as an image). But it seems that the label functionality basically would solve this as well (same syntax as tooltips). So it appears having the labeling possibility for geom_line would solve most of my problems.

@alshan alshan added the * label Nov 22, 2024
@alshan alshan added this to the New milestone Nov 22, 2024
@alshan alshan added the enhancement New feature or request label Nov 22, 2024
@alshan
Copy link
Collaborator

alshan commented Nov 22, 2024

@nsiicm0

My current (hacky) solution for line plots is to use geom_segment and geom_label and I calculate the y-axis coordinate based on the data by hand in order to draw something like this.

I'm not completely sure why geom_segment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
* enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants