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

Discrete minor ticks #5777

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5434.

Briefly, it adds a minor_ticks argument to discrete scales, which I imagine is only useful for position scales.
It controls where minor panel grid lines and minor tick marks are placed, so you can do things like the following:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

ggplot(mpg, aes(class, fill = factor(cyl))) +
  geom_bar(position = position_dodge(preserve = "single")) +
  scale_x_discrete(
    minor_breaks = breaks_width(1, 0.5),
    guide = guide_axis(minor.ticks = TRUE)
  ) +
  theme(
    panel.grid.major.x = element_blank(),
    axis.minor.ticks.length.x.bottom = unit(11, "pt"),
    axis.ticks.length.x.bottom = unit(0, "pt"),
    axis.text.x.bottom = element_text(margin = margin(t = -8.75))
  )

Because the minor breaks are computed on the mapped breaks/limits, rather than on limits, they don't have compatible .value columns that can be combined with discrete .value columns, so the guide drops such incompatible values.

get_guide_data(aesthetic = "x")
#>             x     .value     .label .type y
#> 1  0.08333333    2seater    2seater major 0
#> 2  0.22222222    compact    compact major 0
#> 3  0.36111111    midsize    midsize major 0
#> 4  0.50000000    minivan    minivan major 0
#> 5  0.63888889     pickup     pickup major 0
#> 6  0.77777778 subcompact subcompact major 0
#> 7  0.91666667        suv        suv major 0
#> 8  0.01388889       <NA>       <NA> minor 0
#> 9  0.15277778       <NA>       <NA> minor 0
#> 10 0.29166667       <NA>       <NA> minor 0
#> 11 0.43055556       <NA>       <NA> minor 0
#> 12 0.56944444       <NA>       <NA> minor 0
#> 13 0.70833333       <NA>       <NA> minor 0
#> 14 0.84722222       <NA>       <NA> minor 0
#> 15 0.98611111       <NA>       <NA> minor 0

Created on 2024-03-15 with reprex v2.1.0

As a small note, the minor_breaks argument uses snake_case for consistency with continuous scales rather than dot.case.

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Minor breaks for discrete position scales
2 participants