scales icon indicating copy to clipboard operation
scales copied to clipboard

In label_date_short, can all days, months and years be lined up (regardless of the vjust selected)?

Open davidhodge931 opened this issue 2 years ago • 4 comments

scale_date_short would look better if all the date labels of the same level lined up.

So in the example below, the "Oct" and "Apr" labels would be in line with the "Jan" and "Jul" labels image

davidhodge931 avatar Jun 03 '22 02:06 davidhodge931

This seems like an issue with the vjust of the labels, which in ggplot2 you can change with + theme(axis.text.x.bottom = element_text(vjust = 1)).

teunbrand avatar Jun 03 '22 06:06 teunbrand

That cramps things too close to the axis line and ticks, and forces someone to choose a vjust that is different to what they might have wanted. I.e. they might want vjust of 0.5, but just all comparable label levels on the same line, so they are easy to read, and have nice symmetry

davidhodge931 avatar Jun 03 '22 06:06 davidhodge931

Sometimes you get situations like this...

image

This function would be much improved if all days were able to line-up, all months able to line -up, and all years able to line-up

davidhodge931 avatar Jun 04 '22 06:06 davidhodge931

A way to do this would be to construct breaks so that empty lines are given "", such that all labels have the number of lines of the label with the most line seperators.

In the example above:

  • the label that is "16" above, would instead be a string "16\n\n"
  • the label above that is "06\nJune" would instead be "06\nJune\n"

etc.

davidhodge931 avatar Jun 04 '22 08:06 davidhodge931

This seems to have somehow got fixed, even though no code has been added to main??

library(tidyverse)
economics |> 
  filter(between(date, ymd("1990-01-01"), ymd("1990-03-01"))) |> 
  ggplot() +
  geom_line(aes(date, unemploy)) +
  scale_x_date(labels = scales::label_date_short())

Created on 2023-07-04 with reprex v2.0.2

davidhodge931 avatar Jul 03 '23 21:07 davidhodge931

Huh, interesting. I checked ggplot2 and stringi, but got no clue where this fix was applied :-)

teunbrand avatar Jul 03 '23 21:07 teunbrand

Pretty weird, but great :) Closed the PR as well

Maybe it was grid or vctrs, or something else..

davidhodge931 avatar Jul 03 '23 22:07 davidhodge931