1

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")

graph without rectangle

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

Mel
  • 700
  • 6
  • 31
  • Can you [share your data set](https://stackoverflow.com/a/5963610/5878751)? Or a small part of it? – Nate Jan 16 '20 at 14:13
  • 1
    Thanks, I've just updated the post to show the first 10 rows of the dataset. – Mel Jan 16 '20 at 14:27
  • If having both groups shown is important to the question (I can't tell if it is...), you should include some examples of them both in the sample of data you include—right now you just have FALSE. The link @Nate posted has several suggestions of how to include data in a way that is reproducible – camille Jan 16 '20 at 16:02

1 Answers1

0

I try to simulate something that looks like your dataset, but as @camille said, try to provide a dataset to go with your code? This makes things way faster.

First the data:

DATES = seq(as.POSIXlt("2019-01-01 00:00:00", tz="UTC"), 
    as.POSIXlt("2020-01-31 00:00:00", tz="UTC"), length.out=20)

dataset = data.frame(
  Outcome = c(rpois(20,1000),rpois(20,2000)),
  Week_End_Date = rep(DATES,2),
  Group = rep(c(FALSE,TRUE),each=20)
)
dataset$time_period = ifelse(
dataset$Week_End_Date < as.POSIXlt("2019-11-06 00:00:00", tz="UTC"),
"Baseline",
"Activation"
)

What you need is another data frame that specifies the start and end of your two time periods:

FRAME = dataset %>% group_by(time_period) %>% 
summarize(xmin=min(Week_End_Date),xmax=max(Week_End_Date))

Then similar to what you have done, we can use this in geom_rect, except that now, it comes with a legend:

ggplot(dataset, aes(y=Outcome, x=Week_End_Date, color=Group)) +
  geom_point() +
  geom_line() +
  theme_bw() +
  labs(y="Outcome", x="Date", color="Group", fill="Group")+
  geom_rect(data=FRAME,inherit.aes=FALSE,
  aes(xmin=xmin,xmax=xmax,ymax=+Inf,ymin=-Inf,fill=time_period),alpha=0.1)+
  scale_fill_manual(values=c("red","blue"))

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72