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

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

Closed
Olivia-Box-Power opened this issue Aug 8, 2024 · 6 comments · Fixed by #21
Closed
Assignees
Labels
enhancement New feature or request

Comments

@Olivia-Box-Power
Copy link
Collaborator

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.

@Olivia-Box-Power Olivia-Box-Power added the enhancement New feature or request label Aug 8, 2024
@fingertipsy
Copy link
Collaborator

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
Copy link
Collaborator Author

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

@Olivia-Box-Power
Copy link
Collaborator Author

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

@fingertipsy
Copy link
Collaborator

fingertipsy commented Sep 6, 2024

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
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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

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

Successfully merging a pull request may close this issue.

2 participants