R 包 googleVis 中 gvisTimeline 的自定义工具提示

Custom tooltips for gvisTimeline in the R package googleVis

Question/TL;DR

有没有人在 R 包 googleVis

中为 gvisTimeline 自定义工具提示内容成功

需求范围:

  1. 用解释性文本替换工具提示
  2. 自定义 HTML 工具提示,la https://google-developers.appspot.com/chart/interactive/docs/customizing_tooltip_content_875a2af27d7f8cce657119d51bedda48.frame?hl=en&redesign=true

更新:

我对 gvisTimeline 特别感兴趣,但在 googleVis 包的其他图表中有许多关于工具提示的存根问题。我正在整理这个问题中的这些以供我自己参考,并试图为调查此问题的任何人提供有用的资源:

详情

Google 图表文档清楚地表明工具提示可以针对时间线进行自定义(但不是某些图表):https://developers.google.com/chart/interactive/docs/gallery/timeline and https://developers.google.com/chart/interactive/docs/customizing_tooltip_content.

角色小插图 - https://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html - highlighted here Shiny - googlevis: Tooltips for gvisPieChart 展示了如何为 googlevis 包中的许多图表自定义工具提示,但不包括 gvisTimeline.

检查 github (https://github.com/mages/googleVis/blob/master/R/gvis.R) 上的 gvis 文件表明,包括 tooltip 在内的任何变量都将发送到 Google 图表 API.我盲目地尝试将工具提示包含到 gvisTimeline 图中,如下所示,但无济于事:

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)

我在跌跌撞撞后想通了 https://github.com/mages/googleVis/issues/34

这是实现它的方法。它基本上包括对 gVisTimeline 调用的修改,以将工具提示移交给函数的生成。

  1. 保存并从原始包中获取 gvis.R 到您的工作文件夹(可在 https://cran.r-project.org/web/packages/googleVis/index.html 获得)
  2. 将此修改后的版本保存并源到同一文件夹(它向函数 gvisCheckTimelineData 和 gvisTimeline 添加参数 "tooltips"):

    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)])
    }
    
  3. 将工具提示添加到您的时间轴输入(必须称为 x.tooltips,其中 x 是您的事件或条形标签向量)并将工具提示参数添加到 gVisTimeline 函数。加载 RJSONIO 包(googleVis 中的函数需要)和 googleVis 并享受您的工具提示:

    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)
    

对于 HTML 工具提示,只需使用 HTML 代码作为 datTL 中的字符串(而不是 "cats")并将选项行 options=list(tooltip="{isHtml:'true'}") 添加到 gVisTimeline 通话:

    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)

此致,

桑德罗

我对 Gvistimeline 需要互联网连接感到恼火 re-built 它使用 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")

工具提示取自 "tooltip" 列。更多信息:https://github.com/shosaco/vistime