如何在R中的水平条上绘制时间序列数据?

时间:2018-08-11 14:35:05

标签: r ggplot2 data-visualization

R ::如何绘制单个水平条,以显示从开始日期到当前日期的连续时间序列数据的不同阶段,以及如何在R中水平滚动条导航时间?

This is my data:
    var_events  time_date   event_duration      veh_id
LD  17-06-2018 13:25    6.52                   B33
WL  17-06-2018 13:25    14.52                  B31
TL  17-06-2018 13:26    0.32                   B32
TE  17-06-2018 13:26    4.58                   B13
UL  17-06-2018 13:26    3.45                   B12
WT  17-06-2018 13:26    5.46                   B25
UL  17-06-2018 13:26    1.56                   B17
TL  17-06-2018 13:26    13.6                   B33
SL  17-06-2018 13:26    0.05                   B32

这是上一个代码的折线图示例:

require(ggplot2)
require(dplyr)

df = structure(list(Event_stage = c("SE", "MN", "MN", "TE", "TE", "TE", "TE", "TE", "TE", "TE", "TE", "WL", "TE", "TE", "SE", "TE", "TE", "WL", "WT", "MN", "WL", "TE", "WL", "WL", "WT", "WL", "LD", "WT", "WL", "WT", "WT", "TE", "WL", "LD", "WT", "LD", "MN", "TL", "TE", "WL", "TL", "TL", "WT", "TE", "TE", "LD", "WT", "TL", "LD" ), event_date = structure(c(1529573704, 1529573710, 1529573713, 1529573724, 1529573855, 1529573874, 1529573880, 1529573895, 1529573906, 1529573918, 1529573925, 1529573931, 1529573931, 1529573941, 1529573947, 1529573969, 1529574006, 1529574054, 1529574088, 1529574114, 1529574120, 1529574123, 1529574134, 1529574137, 1529574148, 1529574163, 1529574164, 1529574148, 1529574169, 1529574170, 1529574178, 1529574188, 1529574189, 1529574196, 1529574178, 1529574188, 1529574203, 1529574213, 1529574214, 1529574214, 1529574215, 1529574227, 1529574231, 1529574242, 1529574244, 1529574245, 1529574248, 1529574260, 1529574262), class = c("POSIXct", "POSIXt"), tzone = "UTC"), stage_duration = c(3.78, 3.47, 2.78, 3.45, 3.32, 4.93, 4.23, 4.22, 3.85, 3.37, 5.88, 5.92, 3.97, 3.7, NA, 4.08, 3.05, 0.57, 11.18, 12.08, 2.6, 3.3, 0.23, 0.85, 0.27, 0.25, 0.82, 10.42, 0.15, 0.43, 1.4, 0.25, 0.7, 0.52, 1.12, 0.45, 12.87, 12.18, 2.92, 0.57, 14.07, 12.72, 17.12, 4.13, 3.13, 0.25, 0.33, 18.98, 1.05), veh_id = c("B35", "B05", "B04", "B08", "B14", "B13", "B04", "B17", "B41", "B05", "B26", "B08", "B35", "B19a", "B10a", "B01a", "B28", "B14", "B14", "B18", "B05", "B37", "B04", "B41", "B04", "B19a", "B04", "B17", "B35", "B13", "B35", "B02b", "B28", "B13", "B19a", "B41", "B02b", "B04", "B15", "B01a", "B41", "B13", "B28", "B27", "B33", "B19a", "B01a", "B19a", "B35")), .Names = c("Event_stage", "event_date", "stage_duration", "veh_id"), row.names = c(NA, -49L), class = c("tbl_df", "tbl", "data.frame"))

# create ggplot
ggplot(data = df %>% filter(veh_id == "B35"), aes(x = event_date, 
                      y = stage_duration)) +
  geom_point(aes(color = Event_stage), size= 3) +
  geom_line(alpha = 1/2)+
  labs(x = "Event date", y = "Stage duration")

enter image description here

这是示例条形图,与上面的折线图中的所有内容相同,但不是带有尖峰线的是水平线,或者我只想与滚动条/滚动条交互的单个条来导航时间: enter image description here

类似于此图的内容,但是只有一个水平滚动条以及从开始到当前的滚动条: enter image description here

1 个答案:

答案 0 :(得分:0)

df %>% filter(veh_id == "B35") %>% 
    ggplot(
      aes(
        x = event_date, 
        y = stage_duration)
    ) +
    geom_bar(stat = "identity") +
    labs(x = "Event date", y = "Stage duration") + 
    coord_flip()