Search code examples
rggplot2time-serieszoo

how to plot timeseries events in a barcode style


How to plot a time series of events in a barcode style like the following hand made example?

barcode style time series plot

For the test the following zoo series can be used:

Tdate = c("2020-04-20", "2020-04-22","2020-05-16","2020-05-29", "2020-06-20", "2020-07-02", "2020-07-18", "2020-07-19", "2020-07-22", "2020-09-14", "2020-10-10", "2020-10-15", "2020-11-22", "2020-12-22", "2020-12-24", "2020-12-25")
Tevents = data.frame(station1=c(1,0,0,1,1,1,1,0,0,0,1,1,0,1,0,1),station2=c(1,0,1,1,0,1,1,0,1,1,1,1,0,1,1,1), station3=c(0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0) )
Zevents<-zoo(Tevents,as.Date(Tdate))

Solution

  • You could :

    • pivot_longer the data according to station
    • draw vertical lines with geom_vline
    • use facet_wrap to get a plot per station
    library(tidyr)
    library(dplyr)
    library(ggplot2)
    
    Tevents$dat <- as.Date(Tdate)
    
    
    data <- Tevents %>% pivot_longer(cols = contains('station'), names_to = 'station')
    
    ggplot(data) + geom_point(aes(x = dat, y = 2))+
                   geom_vline(aes(xintercept  = dat), data = filter(data,value == 1)) +
                   coord_cartesian(ylim = c(0,1))+
                   facet_wrap(~station, ncol = 1, strip.position = 'left') +
                   theme(axis.title.y       = element_blank(),
                         axis.text.y        = element_blank(),
                         axis.ticks.y       = element_blank(),
                         panel.grid.minor.y = element_blank(),
                         panel.grid.major.y = element_blank())
    

    enter image description here