Search code examples
rgoogle-visualizationposixctgooglevistimeline.js

Google Vis Timeline aggregation


How can I modify this code to have an aggregated view in the timeline?

I don't want to have three different lines just for avocado. I want to have one line just for when avocado exists. Preferably I even want to have one line for the whole data rather than say one complete line for avocado, one complete line for strawberry and one complete line for blueberry. Any idea is really appreciated.

Besides, any idea how to show those time correctly? The differences between each two time is less than a second but what is shown in measured in year.

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


Solution

  • You just need to divide by the appropriate magnitude and choose your favorite aggregation tool to add the times by group (if I understand you correctly)

    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