teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
534 stars 32 forks source link

[question / feature request] Show only minor tick marks #110

Closed sergiocorreia closed 1 year ago

sergiocorreia commented 1 year ago

HI Teun,

I have a problem that I've been unable to solve for the past days, related to the [guide_axis_minor()] function.

I need to produce charts with ticks every year but where the year labels are centered within each year, as below:

image

(Note how each year's label is centered at the middle of the year)

I can mostly achieve this through the scale_x_date() function:

... +
scale_x_date(minor_breaks=seq(from=as.Date("2003-01-01"), to=as.Date("2023-01-01"), by="1 years"),
               breaks=seq(from=as.Date("2004-06-30"), to=as.Date("2023-06-30"), by="4 years"),
               date_labels="%Y") +
guides(x = ggh4x::guide_axis_minor())

Here, I use minor_breaks to add ticks every year and breaks to add labeled ticks in the middle of each year (minor ticks don't show labels, so this was the only way I could find). However, I can't find a way to hide the mid-year ticks:

image

I've tried setting the x-axis man ticks to zero and then using the ggh4x.axis.ticks.length.minor setting to define the minor ticks, but the latter only takes relative values wrt to the main axis (and anything relative to zero will be zero).

I've also looked at the code a bit and it seems it might be possible to fork the code to use absolute values instead of relative, but I'm not entirely sure where to edit. Lastly, I also saw comments on previous issues regarding the ongoing work on converting ggplot2 guides to ggproto, so I'm wondering if any changes will have to be modified again soon because of this.

Any thoughts or suggestions would be more than appreciated. Thanks! Sergio

teunbrand commented 1 year ago

Hi Sergio,

Thanks for raising the issue. Indeed, it should be more flexible to set the lengths of ticks such that you have more control over this. However, I will probably never fix this in ggh4x and instead deprecate the minor tick functionality. As it happens, I'm trying to get minor ticks into ggplot2 itself: https://github.com/tidyverse/ggplot2/pull/5287. In that PR, the minor tick length is still relative to the theme setting, but so is the major tick length, so you should be able to achieve what you're after here.

If you'd really need that functionality right now, you're able to test that functionality using remotes::install_github("tidyverse/ggplot2", ref = remotes::github_pull("5287")). However, the current state of ggplot2's dev version is incompatible with current ggh4x due to the guide rewrite, so you wouldn't be able to use any of the other functions in ggh4x.

I'll close this issue here in ggh4x, as it won't be changed here.

Best, Teun

sergiocorreia commented 1 year ago

Wow, great news about that PR; hopefully it will get merged!

Also, in case anyone else has the same problem and wants to keep using ggh4x's other functions, I found a stupid trick that somehow worked (but hey, if it's stupid but it works it isn't stupid?): Set axis.ticks.length = unit(-.06, units='pt'), (or any other very low number) and then set ggh4x.axis.ticks.length.minor = rel(100),. This apparently works because 0.06 points is low enough that it doesn't get drawn, but high enough that we can accurately compute 100*.06=6 points. It might not work with other settings (devices, dpis, etc.)

Thanks again, and a great fan of your work!