带有layer_paths的ggvis工具提示

时间:2015-07-08 12:48:00

标签: r ggvis

这是我数据的一个简单示例:

df1 <- structure(
  list(
    X = c(1250, 2500, 3750, 5000, 6250, 7500, 8750,
          10000), Y = c(
            0.112151039933887, 0.0792717402389768, 0.064716676038453,
            0.0560426379617912, 0.0501241024200681, 0.0457556453076907, 0.0423607088430516,
            0.0396242625334144
          )
  ), .Names = c("X", "Y"), row.names = c(NA,-8L), class = "data.frame"
)

我想创建一个带有工具提示的平滑线,显示值X和Y.这就是我现在正在做的事情

library(ggvis)
library(dplyr)
all_values <- function(x) 
{
  if(is.null(x)) return(NULL)
  row <- smoothed[smoothed$id == x$id, ]
  paste0(names(row), ": ", format(row), collapse = "<br />")
}
smoothed <- df1 %>% compute_smooth(Y ~ X) %>% rename(X=pred_ , Y=resp_)
smoothed$id <- 1:nrow(smoothed)
smoothed %>% ggvis(~X, ~Y, key:= ~id, stroke := "red", strokeWidth := 5) %>% layer_paths() %>%
  add_tooltip(all_values, "hover")

工具提示始终显示相同的值。

0 个答案:

没有答案