【问题标题】:how to plot timeseries events in a barcode style如何以条形码样式绘制时间序列事件
【发布时间】:2021-07-17 12:23:57
【问题描述】:

如何以条形码样式绘制事件的时间序列,如下面的手工示例?

对于测试,可以使用以下动物园系列:

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

【问题讨论】:

    标签: r ggplot2 time-series zoo


    【解决方案1】:

    你可以:

    • pivot_longer根据station的数据
    • geom_vline画垂直线
    • 使用facet_wrap 获取每个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())
    

    【讨论】:

    • @user2563989,如果这回答了你的问题,请考虑accepting it
    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 2020-07-12
    • 2016-05-06
    • 2020-02-08
    • 2019-05-25
    • 1970-01-01
    • 2020-01-27
    • 2021-05-14
    相关资源
    最近更新 更多