tidyverts / tsibble

Tidy Temporal Data Frames and Tools
https://tsibble.tidyverts.org
GNU General Public License v3.0
527 stars 49 forks source link

YearWeek and scale_x_yearweek with 1 year gaps lead to wrong x axis. #293

Open USMortality opened 1 year ago

USMortality commented 1 year ago

YearWeek and scale_x_yearweek with 1 year gaps, lead to wrong x axis. See here in this example, 2020 is duplicated twice and every year, thereafter is offset by 1...

I think this has todo with the extra week 53 of 2020... What can I do to fix this?

plot

df <- as_tibble(data.frame(
  x = seq(from = mdy("1/1/2019"), to = mdy("1/1/2022"), by = "week"),
  y = runif(157, min = 0, max = 25)
)) %>% mutate(x = yearweek(x))

ggplot(df, aes(x = x, y = y)) +
  geom_line(color = "#5383EC", linewidth = 1) +
  scale_x_yearweek(date_breaks = "1 year", date_labels = "%Y")
USMortality commented 1 year ago

Workaround, use scale_x_date(date_labels = "%Y", breaks = "1 year") works fine.

df <- as_tibble(data.frame(
  x = seq(from = mdy("1/1/2019"), to = mdy("1/1/2022"), by = "week"),
  y = runif(157, min = 0, max = 25)
)) %>%
  mutate(x = yearweek(x)) %>%
  mutate(date = date(x))

ggplot(df, aes(x = date, y = y)) +
  geom_line(color = "#5383EC", linewidth = 1) +
  scale_x_date(date_labels = "%Y", breaks = "1 year")

plot

So IMO this shows, that there's clearly a bug in the scale_x_yearweek function. Using '52 weeks' as a workaround, appears to make it work, but obviously results in wrong ticks, as there are 53 weeks sometimes...