是否有人成功自定义R包gvisTimeline
googleVis
的工具提示内容
需要范围:
我对gvisTimeline
特别感兴趣,但在googleVis
包的其他图表中有很多关于工具提示的存根问题。我正在将这些问题整理到这个问题中供我自己参考,并试图为所有人提供有用的资源:
gvisTreemap
- https://stackoverflow.com/q/30892289/1659890 Google Charts文档清楚地表明,工具提示可以针对时间轴(但不是某些图表)进行自定义:https://developers.google.com/chart/interactive/docs/gallery/timeline和https://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)
答案 0 :(得分:3)
我在绊倒https://github.com/mages/googleVis/issues/34
之后想出来了以下是如何实现它的方法。它基本上包括对gVisTimeline调用的修改,以便将工具提示移交给函数的生成。
将此修改后的版本保存并发送到同一文件夹(它将参数“工具提示”添加到函数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)])
}
将工具提示添加到时间轴输入(必须称为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)
对于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)
致以最诚挚的问候,
桑德罗
答案 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