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

时间:2015-08-17 20:40:15

标签: r google-visualization

问题/ TL; DR

是否有人成功自定义R包gvisTimeline

googleVis的工具提示内容

需要范围:

  1. 用说明文字替换工具提示
  2. 自定义HTML工具提示,la https://google-developers.appspot.com/chart/interactive/docs/customizing_tooltip_content_875a2af27d7f8cce657119d51bedda48.frame?hl=en&redesign=true
  3. 更新

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

    详细

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

    角色插图 - https://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html - 此处突出显示Shiny - googlevis: Tooltips for gvisPieChart显示如何为googlevis包中的许多图表自定义工具提示,但不包括gvisTimeline。< / p>

    检查github上的gvis文件(https://github.com/mages/googleVis/blob/master/R/gvis.R)会显示包含tooltip的任何变量都会发送到Google Chart 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)
    

    enter image description here

2 个答案:

答案 0 :(得分:3)

我在绊倒https://github.com/mages/googleVis/issues/34

之后想出来了

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

  1. 将gvis.R从原始包保存并下载到您的工作文件夹(可在https://cran.r-project.org/web/packages/googleVis/index.html下载)
  2. 将此修改后的版本保存并发送到同一文件夹(它将参数“工具提示”添加到函数gvisCheckTimelineData和gvisTimeline):

    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是事件或barlabel向量)和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)
    
  4. minimal example

    对于HTML工具提示,只需将HTML代码用作datTL中的字符串(而不是“猫”),并将选项行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)
    

    HTML Tooltips

    致以最诚挚的问候,

    桑德罗

答案 1 :(得分:1)

我很恼火Gvistimeline需要互联网连接并使用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")

工具提示取自“工具提示”列。更多信息:https://github.com/shosaco/vistime enter image description here