r-lib / scales

Tools for ggplot2 scales
https://scales.r-lib.org
Other
407 stars 109 forks source link

Feature request: an option to remove leading zeros in `label_date_short()` #442

Closed lz1nwm closed 4 weeks ago

lz1nwm commented 4 months ago

Is it possible to implement an option to remove leading zeros in label_date_short()?

Untitled1

teunbrand commented 1 month ago

The format.Date() via strptime() does not support no leading characters. You can replace the zero with a space though:

x <- seq(Sys.Date(), Sys.Date() + 365, by = "1 month")
labels <- scales::label_date_short(format = c("%Y", "%b", "%e", "%H:%M"))
labels(x)
#>  [1] " 3\nOct\n2024" " 3\nNov"       " 3\nDec"       " 3\nJan\n2025"
#>  [5] " 3\nFeb"       " 3\nMar"       " 3\nApr"       " 3\nMay"      
#>  [9] " 3\nJun"       " 3\nJul"       " 3\nAug"       " 3\nSep"      
#> [13] " 3\nOct"

Created on 2024-10-03 with reprex v2.1.1

There is also a rumour that you can use "%-d" on non-Windows platforms can format the day without leading 0, but I can't verify this at the moment.

lz1nwm commented 1 month ago

Thanks, I know that but it doesn't look centered. I thought that it could be as simple as implementing something like:

gsub("^0", "", format(as.Date("2024-09-02"), "%m\n%Y"))

...but maybe it's not