【发布时间】:2015-08-08 20:22:10
【问题描述】:
我想使用 ggvis 中的工具提示功能为曲线上的特定点创建悬停文本。我可以让情节形成,但悬停字段中的文本不会显示。当我尝试添加不应被视为可视化交互部分的一部分的背景层时,会发生这种情况。下面是一些代码说明:
library(ggvis)
# one-compartment oral concentration curve
comp1.oral <- function(ka,ke,v,f,dose,time){
(ka * dose * f)/ (v*(ka-ke)) * (exp(-ke * time) - exp(-ka*time))
}
time <- 0:200 # time points to create curve
tp <- 6 # number of times to sample
tmax <- max(time)
#generically choosing tp points to sample at
tnew <- exp(seq(0,log(tmax),length=(tp)))
#computing the concentration (y value)
y <- comp1.oral(.1,.03,4,1,100,tnew)
#creating dataframe with values
# PK and ECG should be in the hover text
d1 <- data.frame(
Conc= y,
Time=tnew,
PK = 1:tp,
ECG= "No"
)
# creating a column with the text to appear in the hover box
d1$long <- paste0("PK: ",d1$PK,"<br>","ECG: ",d1$ECG,"<br>")
#creating another data frame to input the time-conc curve as a background layer
d2 <- data.frame(
x=time,
y=comp1.oral(.1,.03,4,1,100,time)
)
下面的代码将形成我想要的情节,但没有悬停文本。
d1 %>%
ggvis(x = ~Time, y=~Conc) %>%
layer_points(size.hover:=200) %>%
layer_paths(~x,~y,data=d2) %>%
add_tooltip(function(d1){
if (!is.null(d1$Time)) paste0("PK:", "<br>ECG:", "<br>Time: ", as.character(round(d1$Time)), " minutes post-dose")
}, "hover")
我想从 d1$long 获取 otehr 值到悬停文本框中。我尝试添加类似于在烂番茄闪亮示例中看到的内容,但它不起作用。
我尝试了以下,但似乎无法在变量 d1$long 中找到附加文本
d1 %>%
ggvis(x = ~Time, y=~Conc, key := ~long) %>%
layer_points(size.hover:=200) %>%
layer_paths(~x,~y,data=d2) %>%
add_tooltip(function(d1){
if (!is.null(d1$Time)) paste0(as.character(d1$long),"Time: ", as.character(round(d1$Time)), " minutes post-dose")
}, "hover")
【问题讨论】:
标签: r interactive ggvis