Search code examples
rcsvgoogle-visualizationvisualizationgooglevis

How to use google motionchart or scatterchart for string data type in R?


My data types are string so I wonder how can I use one of the Google motion chart or Google scatter chart for this purpose? I want my y-axis to show one of the fruits and the x-axis to show the timestamp. I got this error:

> motion
   dd.rosbagTimestamp    dd.data
1 1438293900729698553 strawberry
2 1438293901681590725    avocado
3 1438293904496769068    avocado
4 1438293943211221553  blueberry
5 1438293963216807017       kiwi
> gvisScatterChart(motion)
Error in gvis(type = type, checked.data, options = options, chartid = chartid,  : 
  Only the following data types are allowed: number
However, dd.rosbagTimestamp, dd.data is of type string, string

The problem is what would you do in googlevis if you have data in string format like fruit names? enter image description here


Solution

  • I want to point out a few things before getting into a solution:

    1. I'm not sure who put this data set together, but they either messed something up with the timestamps, or left out some very key information on how to decipher them, for the reasons noted in my comments above.
    2. I used what I felt was the most (re: not necessarily correct) appropriate transformation for the 19 character timestamps - i.e. taking the first 10 digits to be the number of seconds since the Unix epoch; but even though this data set is very strange. 5000+ observations with less than 300 unique timestamps (using the 10 digits method)?
    3. I don't know if this is a course assignment or something similar and you were instructed to use either gvisScatterChart or gvisMotionChart, but neither are particularly appropriate for this data set IMO. I went with the latter of the two; I'm not sure it's possible to represent your data with the former. Regardless, you should keep in mind that how you present your data is at least is important as what you are presenting. googleVis offers some neat tools which are sometimes very useful, but I personally find the interface to be a bit too restrictive (probably due to the Google Charts API, and not necessarily the fault of the package developers). In this case, gvisMotionChart does not seem to let you plot anything less atomic than daily observations, so I had to transform 1 second gaps to 1 day gaps.

    Anyways, given the issues noted above, here's one possible approach:


    library(data.table)
    library(googleVis)
    ##
    mdt <- data.table(motion)
    gdt <- mdt[
      ,.(frequency = .N), 
      keyby = "tstamp,fruit"]
    
    gdt[
      ,time_value := as.numeric(
        tstamp - min(gdt$tstamp))]
    gdt[
      ,scaled_date := min(
        as.Date(gdt$tstamp)) + time_value]
    ##
    plot_data <- data.frame(
      idvar = gdt$fruit,
      timevar = gdt$scaled_date,
      frequency = gdt$frequency,
      fruit = gdt$fruit)
    ##
    gmc <- gvisMotionChart(
      data = plot_data,
      idvar = "idvar",
      timevar = "timevar",
      yvar = "frequency",
      colorvar = "fruit",
      date.format = "%Y-%m-%d")
    ##
    R> plot(gmc)
    

    This is a snapshot of the motion chart: enter image description here


    And the raw data, read in from a CSV file in your link:


    Df <- read.csv(
      file = "~/tmp/gazedata.csv.txt",
      sep = ",", header = TRUE,
      colClasses = "character",
      stringsAsFactors = FALSE
    )
    ##
    motion <- data.frame(
      tstamp = as.POSIXct(
        as.numeric(substr(Df[,1], 1, 10)),
        tz = "UTC",
        origin = "1970-01-01 00:00:00"),
      fruit = Df$data
    )