best-practice-and-impact / afcharts

https://best-practice-and-impact.github.io/afcharts/
Other
9 stars 0 forks source link

Function to make top horizontal gridline always above the highest datapoint #8

Closed Olivia-Box-Power closed 2 weeks ago

Olivia-Box-Power commented 3 months ago

I think having the top horizontal gridline above the highest data point can make the data easier to read. I'm not sure if this is documented in the AF charts guidance though. Gridlines are easy to set manually when you know what the data values are, but are difficult to automate.

fingertipsy commented 2 months ago

scale defaults cannot be updated in the same way we do for geom defaults. It requires a lengthy wrapper function that checks what kind of scale is present and replaces it with the "pretty" version of that scale. Maybe instead of automating this, we can point to and provide guidance on using the existing scales package?

Olivia-Box-Power commented 2 months ago

@fingertipsy Could we not write a function to be passed to the breaks argument of scale_y_continuous?

Olivia-Box-Power commented 2 months ago

Or perhaps we could submit a feature request on the ggplot2 repo?

fingertipsy commented 2 months ago

yes, we could have a function that does something like:

limits_pretty <- function(x, ...) range(pretty(x, ...))

ggplot(data, mapping, ...) + scale_y_continuous(breaks = pretty, limits = limits_pretty)

I could not find a way to pass this down as a default via the existing theme_af() or use_afcharts(). So the only options I could think of were:

  1. a standalone function that adds scale_y_continuous(breaks = pretty, limits = limits_pretty) to a ggplot.
  2. a wrapper function around ggplot, e.g. af_ggplot(data, mapping, ...)

Neither seemed like a great idea. 1.) is basically a small subset of what you can do using the scales package. You have to write the extra line of code to set the axis either way. Not sure, it would add much for users to have a function afcharts::pretty_y_axis()? 2.) seemed like a bad idea, because it would very much change the scope of the package.

Happy to discuss at the next meeting

Olivia-Box-Power commented 1 month ago

Thanks. I agree, I don't think this is something we should add to the package.

Could you update the cookbook please with an example of how the user can use the following to fix the problem?


limits_pretty <- function(x, ...) range(pretty(x, ...))

ggplot(data, mapping, ...) +
scale_y_continuous(breaks = pretty, limits = limits_pretty)

I'll create an issue on the ggplot2 package and see if this is an issue that could be added.

Olivia-Box-Power commented 1 month ago

This was the issue I raised on ggplot2. They recommend Stefan's answer.