First I created sample data. Hope this looks like your problem:
df = data.frame(id=rep(1:9), A=c(12,13,13,14,12,11,12,11,10), B=c(13,12,10,12,6,9,10,11,12), Type=c("F","R","F","R","W","F","R","F","R"), datetime=as.POSIXct(c("2015-01-01 01:00:00","2015-01-01 22:50:00", "2015-01-02 08:30:00","2015-01-02 23:00:00", "2015-01-03 14:10:00","2015-01-05 16:30:00", "2015-01-05 23:00:00","2015-01-06 17:00:00", "2015-01-07 23:00:00")), stringsAsFactors = F)
Your first question is to build the data by highlighting the first 24 hours after the F-point. For this task, I used dplyr and ggplot.
library(dplyr) library(ggplot) df %>% mutate(nf = cumsum(Type=="F")) %>%
The problem here is that color changes only at some points. The only thing that doesnβt suit me is the use of different line colors for the path sections. If first24h is a discrete variable geom_path draws two sepearate paths. Therefore, I defined the variable as numeric. Maybe someone can improve this?
The second question about interpolation can be easily solved with the zoo package:
library(zoo) full.time = seq(df$datetime[1], tail(df$datetime, 1), by=600)
With these two data frames, you get a solution. Each FF section is drawn in a separate graph, and only points are displayed that do not exceed 24 hours after the F point is displayed.
df %>% select(Type, datetime) %>% right_join(d.full, by="datetime") %>% mutate(Type = ifelse(is.na(Type),"",Type)) %>% mutate(nf = cumsum(Type=="F")) %>% group_by(nf) %>% mutate(first24h = (datetime-min(datetime)) < (24*3600)) %>% filter(first24h == TRUE) %>% mutate(lbl=paste0(row_number(),"-",Type)) %>% filter(first24h == 1) %>% ggplot(aes(x=A, y=B, label=Type)) + geom_path() + geom_text() + facet_wrap(~ nf)
