Custom hints for gvisTimeline in googleVis R package

Question / TL; DR

Has anyone succeeded in customizing the contents of the tooltip for gvisTimeline in the googleVis R googleVis

Scope of application:

Update:

I'm particularly interested in gvisTimeline , but there are a lot of questions about tooltips in other googleVis package diagrams. I am comparing them in this question for my own reference and in an attempt to provide a useful resource for anyone looking at this:

More details

The Google Charts documentation makes it clear that tooltips are customizable for timelines (but not for some charts): https://developers.google.com/chart/interactive/docs/gallery/timeline and https://developers.google.com / chart / interactive / docs / customizing_tooltip_content .

Role Vignettes - https://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html - highlighted here Shiny - googlevis: Tooltips for gvisPieChart shows how tooltips can be configured for many diagrams in the googleVis package but do not include gvisTimeline .

Checking the gvis file on github ( https://github.com/mages/googleVis/blob/master/R/gvis.R ) shows that any variable, including tooltip , will be sent to the Google Charts API. Blindly, I tried to include prompts in the gvisTimeline graph as follows, but to no avail:

 datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)), Name=c("Washington", "Adams", "Jefferson", "Adams", "Jefferson", "Burr"), start=as.Date(x=rep(c("1789-03-29", "1797-02-03", "1801-02-03"),2)), end=as.Date(x=rep(c("1797-02-03", "1801-02-03", "1809-02-03"),2)), Position.html.tooltip=c(rep("cats",6))) Timeline <- gvisTimeline(data=datTL, rowlabel="Name", barlabel="Position", start="start", end="end") plot(Timeline) 

enter image description here

+5
source share
2 answers

I figured this out after stumbling https://github.com/mages/googleVis/issues/34

Here is how you can achieve this. It basically involves modifying the gVisTimeline call to send tooltips to generate a function.

  • save the original gvis.R from the source package to your working folder ( https://cran.r-project.org/web/packages/googleVis/index.html is available)
  • save and load this modified version in the same folder (it adds the "tooltips" parameter to the gvisCheckTimelineData and gvisTimeline functions):

     gvisTimeline <- function(data, rowlabel="", barlabel="", tooltip="", start="",end="", options=list(), chartid){ my.type <- "Timeline" dataName <- deparse(substitute(data)) my.options <- list(gvis=modifyList(list(width=600, height=200),options), dataName=dataName,data=list(rowlabel=rowlabel, barlabel=barlabel,tooltip=tooltip, start=start, end=end,allowed=c("number", "string", "date", "datetime")) ) checked.data <- gvisCheckTimelineData(data, rl=rowlabel, bl=barlabel, tt=tooltip, start=start, end=end) output <- gvisChart(type=my.type, checked.data=checked.data, options=my.options,chartid=chartid, package="timeline") return(output) } gvisCheckTimelineData <- function(data, rl, bl, tt, start, end){ if(any(c(rl, bl, tt, start, end) %in% "")) return(data) else return(data[, c(rl, bl, tt, start, end)]) } 
  • Add tools to your timeline input (you need to call x.tooltips, where x is your event or barclab vector) and a tooltip parameter for the gVisTimeline function. Download the RJSONIO package (required for the function inside googleVis) and googleVis and enjoy the hints:

     library(googleVis) library(RJSONIO) source("gvis_orig.R") source("gvis_mod_for_tooltips.R") datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)), Name=c("Washington", "Adams", "Jefferson", "Adams", "Jefferson", "Burr"), start=as.Date(x=rep(c("1789-03-29", "1797-02-03", "1801-02-03"),2)), end=as.Date(x=rep(c("1797-02-03", "1801-02-03", "1809-02-03"),2)), Position.html.tooltip=c(rep("cats",6))) Timeline <- gvisTimeline(datTL, rowlabel="Name", barlabel="Position", start="start", end="end", tooltip="Position.html.tooltip") plot(Timeline) 

minimal example

For HTML hints, just use the HTML code as a string in datTL (instead of "cats") and add the option line options=list(tooltip="{isHtml:'true'}") to gVisTimeline call:

  library(googleVis) library(RJSONIO) source("gvis_orig.R") source("gvis_mod_for_tooltips.R") datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)), Name=c("Washington", "Adams", "Jefferson", "Adams", "Jefferson", "Burr"), start=as.Date(x=rep(c("1789-03-29", "1797-02-03", "1801-02-03"),2)), end=as.Date(x=rep(c("1797-02-03", "1801-02-03", "1809-02-03"),2)), Position.html.tooltip=c(rep('<a href="http://www.r-project.com"><img src="http://www.r-project.org/Rlogo.jpg" alt="R logo" /></a>',6))) Timeline <- gvisTimeline(datTL, rowlabel="Name", barlabel="Position", start="start", end="end", tooltip="Position.html.tooltip", options=list(tooltip="{isHtml:'true'}")) plot(Timeline) 

HTML Tooltips

Respectfully,

Sandro

+3
source

It annoyed me that Gvistimeline needed an Internet connection and rebuilt it with plotly :

 install.packages("vistime") library(plotly) library(vistime) datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)), Name=c("Washington", "Adams", "Jefferson", "Adams", "Jefferson", "Burr"), start=as.Date(x=rep(c("1789-03-29", "1797-02-03", "1801-02-03"),2)), end=as.Date(x=rep(c("1797-02-03", "1801-02-03", "1809-02-03"),2)), color=c(rep("blue", 3), rep("red", 3)), fontcolor=rep("white",6), tooltip=c(rep("cats",6))) vistime(datTL, events="Position", groups="Name", title="Presidents of the USA") 

Hints are taken from the "hint" column. Additional information: https://github.com/shosaco/vistime enter image description here

+1
source

All Articles