使用ggvis

时间:2015-05-26 23:38:29

标签: r interactive ggvis

我想使用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")

1 个答案:

答案 0 :(得分:0)

错误是变量应该在layer_points中传递

d1 %>% 
  ggvis(x = ~Time, y=~Conc) %>%
  layer_points(size.hover:=200, key := ~long) %>% 
  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")