Google Vis时间轴聚合

时间:2015-08-06 04:37:27

标签: r google-visualization posixct googlevis timeline.js

如何修改此代码以在时间轴中显示聚合视图?

我不想为鳄梨提供三种不同的产品线。我想在鳄梨存在时只有一条线。我甚至想要为整个数据提供一条生产线,而不是说一条完整的鳄梨生产线,一条完整的草莓生产线和一条完整的蓝莓生产线。任何想法都非常感激。

此外,任何想法如何正确显示这些时间?每两次之间的差异小于一秒,但以年度为单位显示。

library("googleViz")
dd <- read.csv(header = TRUE, text = "rosbagTimestamp,data
1438293919014802388,avocado
1438293919078955343,avocado
1438293919082352685,avocado
1438293919146142553,0
1438293919177955753,0
1438293919244013175,strawberry
1438293919251252990,strawberry
1438293919322521358,blueberry
1438293919327731275,blueberry")

dd <- within(dd, {
  end <- as.POSIXct(as.numeric(substr(rosbagTimestamp, 1, 10)),
                    origin = '1970-01-01')
  start <- as.POSIXct(as.numeric(substr(rosbagTimestamp, 11, 19)),
                      origin = '1970-01-01')
  rosbagTimestamp <- NULL
})

#         data               start                 end
# 1    avocado 1970-06-21 03:47:12 2015-07-30 18:05:19
# 2    avocado 1972-07-02 16:01:04 2015-07-30 18:05:19
# 3    avocado 1972-08-10 23:44:00 2015-07-30 18:05:19
# 4          0 1974-08-19 07:07:44 2015-07-30 18:05:19
# 5          0 1975-08-22 12:10:40 2015-07-30 18:05:19
# 6 strawberry 1977-09-25 01:24:16 2015-07-30 18:05:19
# 7 strawberry 1977-12-17 19:29:52 2015-07-30 18:05:19
# 8  blueberry 1980-03-21 16:15:44 2015-07-30 18:05:19
# 9  blueberry 1980-05-21 00:26:40 2015-07-30 18:05:19

plot(gvisTimeline(dd, rowlabel = 'data', barlabel = 'data',
                  start = 'start', end = 'end'))

enter image description here

1 个答案:

答案 0 :(得分:2)

你只需要除以适当的幅度并选择你喜欢的聚合工具来按组添加时间(如果我理解正确的话)

library('googleVis')
dd <- read.csv(header = TRUE, text = "rosbagTimestamp,data
               1438293919014802388,avocado
               1438293919078955343,avocado
               1438293919082352685,avocado
               1438293919146142553,0
               1438293919177955753,0
               1438293919244013175,strawberry
               1438293919251252990,strawberry
               1438293919322521358,blueberry
               1438293919327731275,blueberry")

dd <- within(dd, {
  end <- as.POSIXct(as.numeric(substr(rosbagTimestamp, 1, 10)) / 1e8,
                    origin = '1970-01-01')
  start <- as.POSIXct(as.numeric(substr(rosbagTimestamp, 11, 19)) / 1e8,
                      origin = '1970-01-01')
  rosbagTimestamp <- NULL
})

## sum the times by group
dd1 <- aggregate(. ~ data, data = dd, sum)
dd1 <- within(dd1, {
  start <- as.POSIXct(start, origin = '1970-01-01')
  end <- as.POSIXct(end, origin = '1970-01-01')
})

#         data               start                 end
# 1          0 1969-12-31 19:00:03 1969-12-31 19:00:28
# 2    avocado 1969-12-31 19:00:01 1969-12-31 19:00:43
# 3  blueberry 1969-12-31 19:00:06 1969-12-31 19:00:28
# 4 strawberry 1969-12-31 19:00:04 1969-12-31 19:00:28

plot(gvisTimeline(dd1, rowlabel = 'data', barlabel = 'data',
                  start = 'start', end = 'end'))

enter image description here