Adding vertical line to graph of weekly data

Is it possible to add a vertical line to a ggplot graph with data of class Week on the x-axis? For example

library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.3
DF <- data.frame(dt = yearweek("2000W01") + 0:20, val = 0:20)
ggplot(DF, aes(x=dt, y=val)) + 
  geom_point() + 
  geom_vline(xintercept = as.numeric(yearweek("2000 W10")))

Created on 2022-05-11 by the reprex package (v2.0.1)

Note that no error message appears; the graph simply appears without any vertical line.

Any help greatly appreciated.

immediate solution

library(tsibble)
library(ggplot2)
DF <- data.frame(dt = as.Date(yearweek("2000W01") + 0:20), val = 0:20)
ggplot(DF, aes(x=dt, y=val)) + 
  geom_point() + 
  geom_vline(xintercept = as.Date(yearweek("2000 W10"))) +
  scale_x_date(date_breaks = "4 weeks",date_labels = "%Y W%W")

I suppose it would be preferable to not need to map the yearweek to Date and further modify, but it seems the tsibble folks would need to implement more on their side to make a direct yearweek approach.

library(tsibble)
DF <- data.frame(dt = yearweek("2000W01") + 0:20, val = 0:20)
ggplot(DF, aes(x=dt, y=val)) + 
  geom_point() + 
  geom_vline(xintercept = yearweek("2000 W10"))

Error in UseMethod("rescale") : 
  no applicable method for 'rescale' applied to an object of class "c('yearweek', 'vctrs_vctr')"

The tsibble issues board is here :
Issues ยท tidyverts/tsibble (github.com)

Thanks, this seems to work.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.