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

Feature Request: Permanent Tooltip / Annotate last datapoint

Open nsiicm0 opened this issue 1 year ago • 5 comments

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()"?

nsiicm0 avatar Nov 14 '24 15:11 nsiicm0

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,

syd-doyen avatar Nov 19 '24 12:11 syd-doyen

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.

alshan avatar Nov 19 '24 22:11 alshan

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.

nsiicm0 avatar Nov 20 '24 10:11 nsiicm0

@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?

alshan avatar Nov 22 '24 18:11 alshan

@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?

So I can draw an arrow to point to the data point.

nsiicm0 avatar Nov 26 '24 14:11 nsiicm0

Related:

Image

alshan avatar Aug 04 '25 17:08 alshan