nhs-r-community / NHSRplotthedots

An SPC package to support NHSE/I 'Making Data Count' programme
https://nhs-r-community.github.io/NHSRplotthedots/
Other
48 stars 23 forks source link

Consider adding annotations for UCL, LCL and mean #141

Closed mauriziopassariello closed 1 year ago

mauriziopassariello commented 2 years ago

Maybe this is a feature that already exists and I don't know(if so apologies!)...it would be useful to have the control limits and the mean displayed on the chart next/above the lines.

ThomUK commented 2 years ago

I think this would be a good idea too, and something I have occasionally needed to make some commentary clear. Turn off and on-able with an argument to ptd_create_ggplot(), perhaps? Do you fancy putting together a PR?

I think the challenges are handling overplotting with points / plot boundaries, and also creating a clean API to handle labelling when the limits have been re-based (perhaps labelling first and most recent limits only to enable commentary on improvement shown by the chart?).

mauriziopassariello commented 2 years ago

Hi Tom, adding an argument to turn this feature on/off in ptd_create_ggplot() may be the way to go. IT skills are limited but happy to test it out if someone comes up with something like this.

ThomUK commented 2 years ago

Sounds good - thanks for raising the issue!

francisbarton commented 1 year ago

The NHS-R newscast alerted me to this issue :-) Then I was just reading this presentation (Future NHS link) and noticed that they have used a secondary y-axis to show the limits and mean. Though this would get messy if you tried including these for multiple sets after a rebase, as in Tom's comment 2 above. Though it handles the issue of overplotting well. I don't love it but it's an option.

ThomUK commented 1 year ago

It would be nice to avoid a secondary axis if possible.
I was thinking of 4 separate annotations (LCL, Mean, UCL, and target), aligned vertically to their dotted line, and horizontally wherever they cause the fewest problems. Probably on the right so that the most recent (post-rebase) information is covered.

As Tom found with the icons, there is going to be overlapping with some chart combinations, which is hard to avoid without introducing a border for the annotations to sit in.

tomjemmett commented 1 year ago

the secondary axis way of labelling is a really neat approach and probably the way I would attack this, it's certainly what I've used in the past. In summary (at least, what I did): your secondary axis has the same limits as the primary axis, but different breaks: you set the breaks to be at lcl, mean, ucl, target (rather than a regular interval of +x from last tick). The labels then would use a function from {scales} to neatly format the values.

There are some issues that may arise from this: 1) this approach only works on the last point of data, so won't tell you the values if you rebase (actually, I would argue this is a feature rather than an issue) 2) it reduces the plot area, with the icons these are positioned absolutely in a (specified) corner of the plot area. As with point 1, the plot area is reduced so we may have more overlapping of icons with data points 3) this will likely not work with facets. It requires independent axes (I think we specify axis="free" though?). Same issue again as point 1, plot area is reduced, which is going to be an issue with facets as you are already cramming a lot of information into one space

The alternative is to use something like geom_annotate to add values into the plot area: this I feel is something that users should do themselves, as you will likely require playing about on case by case basis to perfectly position your labels to prevent overlapping. Perhaps a vignette could show this method off, but leave it at that.

francisbarton commented 1 year ago

PR #188 was created to address this issue, and that has now been merged into main. Do people on this thread want to keep the issue open for further discussion or enhancement of the fix? In my mind we can close this now, but there are still potential design/aesthetic questions about the whole secondary axis solution, if you want to have them!

ThomUK commented 1 year ago

Thanks for the work on the PR Fran! Have seen the proposed docs updates in #192 Similarly any tweaks would be best proposed in a new issue, so closing this one now... Thank you!