I have a dataset as follows:
# A tibble: 30 x 4
# Groups: Group, Week_End_Date [30]
Group Week_End_Date time_period Outcome
<lgl> <dttm> <fct> <dbl>
1 FALSE 2019-09-22 00:00:00 Baseline 5241.
2 FALSE 2019-09-29 00:00:00 Baseline 5089.
3 FALSE 2019-10-06 00:00:00 Baseline 4991.
4 FALSE 2019-10-13 00:00:00 Baseline 4920.
5 FALSE 2019-10-20 00:00:00 Baseline 4896.
6 FALSE 2019-10-27 00:00:00 Baseline 4921.
7 FALSE 2019-11-03 00:00:00 Baseline 4999.
8 FALSE 2019-11-10 00:00:00 Activation 5003.
9 FALSE 2019-11-17 00:00:00 Activation 4995.
10 FALSE 2019-11-24 00:00:00 Activation 5098.
# ... with 20 more rows
I can plot a graph e.g.
ggplot(dataset, aes(y=Outcome, x=Week_End_Date, color=Group, fill=Group)) +
geom_point() +
geom_line() +
theme_bw() +
labs(y="Outcome", x="Date", color="Group", fill="Group")
I want to shade this based on the variable "time_period" (Week_End_Date is nested within this)
e.g.
ggplot(dataset, aes(y=Outcome, x=Week_End_Date, color=Group, fill=Group)) +
geom_point() +
geom_line() +
theme_bw()
geom_line() +
theme_bw() +
geom_rect(aes(fill=time_period, xmax=Inf, xmin=-Inf, ymax=Inf, ymin=-Inf))
however this results in the error message
Error: Invalid input: time_trans works with objects of class POSIXct only
I also tried drawing the two rectangles separately (I will add the alpha argument later once they plot successfully):
ggplot(dataset, aes(y=Outcome, x=Week_End_Date, color=Group, fill=Group)) +
geom_point() +
geom_line() +
theme_bw()
geom_line() +
theme_bw() +
geom_rect(inherit.aes=FALSE, aes(xmin=dmy("06Nov2019"), xmax=dmy("06Jan2020"), ymax=Inf, ymin=-Inf), fill="red") +
geom_rect(inherit.aes=FALSE, aes(xmin=dmy("01Jan2019"), xmax=dmy("06Nov2019"), ymax=Inf, ymin=-Inf), fill="blue")
but this results in the same error.
Can anyone advise me on what I'm doing wrong? I feel like this should be far more straightforward than it seems! The x variable is a datetime