Search code examples
rgoogle-visualizationshinygooglevis

Creating a gvisLineChart plot as a discrete Scatter chart


I am trying to create a scatter plot where the input data to the x-axis is string based. gvisScatterChart doesn't support this data type, and I have implemented a gvisLineChart instead.

The problem is that the data is plotted with each data having a seperate x-axis position, where as I would like the points to be discrete, only one x-axis position for strings that are equal.

Any ideas how to solve this? I've provided a minimal example of my plotting. It is used in a Shiny application, however the problem is with the plotting.

I've update the minimal example to be working, from R directly. The date is just one example, where the data input comes in string type. (So just make it work for the date is not enough.

require(googleVis)

dataPlot <- data.frame(date = c("03/03/2015","03/03/2015",
                                "06/03/2015","06/03/2015","09/03/2015"), 
                       results = rnorm(5), results.html.tooltip = rnorm(5))

line <- gvisLineChart(dataPlot, xvar="date", yvar=c("results","results.html.tooltip"),
                 options=list(legend="none",
                              lineWidth=0,
                              pointSize=8,
                              width=400, 
                              height=400))

plot(line)

Solution

  • I solved this by shaping the data correctly, splitting it by date. This has however not solved my tooltip problem yet. Here's my solution:

    cbind.fill <- function(...){
      nm <- list(...) 
      nm<-lapply(nm, as.matrix)
      n <- max(sapply(nm, nrow)) 
      do.call(cbind, lapply(nm, function (x) 
        rbind(x, matrix(, n-nrow(x), ncol(x))))) 
    }
    
    dataPlot <- data.frame(xdata = sort(unique(dataPlot$date)),
                         ydata = t(do.call(cbind.fill,
                                        lapply(split(dfin,dfin$dut),
                                               "[",c("results")))),
                         row.names = NULL)