Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom tooltips for gvisTimeline in the R package googleVis

Question/TL;DR

Has anyone had success in customising the tooltip content for gvisTimeline in the R package googleVis

Scope of need:

  1. Replacing tooltip with explanatory text
  2. Custom HTML tooltip, a la https://google-developers.appspot.com/chart/interactive/docs/customizing_tooltip_content_875a2af27d7f8cce657119d51bedda48.frame?hl=en&redesign=true

Update:

I am specifically interested in gvisTimeline but there are a number of stub questions about tooltips in the other charts of the googleVis package. I'm collating these in this question for my own reference and in an attempt to provide a useful resource for anyone looking into this:

  • gvisTreemap - https://stackoverflow.com/q/30892289/1659890

Details

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

The roles 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 customised for many of the charts in the googlevis package but does not include gvisTimeline.

Inspecting 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 Chart API. Blindly I attempted to include tooltips into a gvisTimeline plot 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

like image 714
Charlie Joey Hadley Avatar asked Sep 28 '22 00:09

Charlie Joey Hadley


1 Answers

I was annoyed of Gvistimeline needing internet connection and re-built it using 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")

Tooltips are taken from "tooltip" column. More information: https://github.com/shosaco/vistime enter image description here

like image 185
shosaco Avatar answered Dec 24 '22 13:12

shosaco