11

这是我的输入数据集:

> names(breakingbad.episodes)
[1] "season"           "episode"          "epnum"            "epid"             "title"           
[6] "url.trakt"        "firstaired.utc"   "id.tvdb"          "rating"           "votes"           
[11] "loved"            "hated"            "overview"         "firstaired.posix" "year"            
[16] "zrating.season"   "src"     

对于我ggvis,我使用以下变量firstaired.posixrating

> str(breakingbad.episodes[c("firstaired.posix", "rating")])
'data.frame':   62 obs. of  2 variables:
$ firstaired.posix: POSIXct, format: "2008-01-21 02:00:00" "2008-01-28 02:00:00" "2008-02-  11 02:00:00" ...
$ rating          : num  87 85 84 84 83 90 87 85 88 83 ...

我成功创建了我ggvis的工具提示,其中包含如下rating信息:

> breakingbad.episodes %>% 
ggvis(x = ~firstaired.posix, 
    y = ~rating, 
    fill = ~season) %>% 
layer_points() %>%
add_axis("x", title = "Airdate") %>%
add_axis("y", title = "Rating") %>%
add_legend("fill", title = "Season") %>%
add_tooltip(function(data){paste0("Rating: ", data$rating)}, "hover")

但我实际上希望工具提示包含更多数据,比如epid变量,所以我尝试了:

…
add_tooltip(function(data){paste0("Rating: ", data$rating, "\n", "Epid: ", as.character(data$epid))}, "hover")

…使用as.character()因为epid是一个有序的因素——但工具提示的部分是空的。(我还注意到我打算\n插入的换行符丢失了,但这是一个不同的问题)。

看起来这个问题的原因是vis通过管道将我的数据集创建的对象ggvis不包含我想要显示的信息,至少这就是我通过查看str()第一个示例的输出来收集的原因。

编辑:我解决了换行问题,所以没有必要指出我?add_tooltip——完全忘记了这一点。

编辑:接受的答案工作正常,即使它不允许我在工具提示中放置任意变量,这几乎是我的用例所需要的,谢谢!这是我最后所做的:

breakingbad.episodes <- transform(breakingbad.episodes, id = paste0(epid, " - ", title))

breakingbad.episodes %>% 
  ggvis(x = ~firstaired.posix, 
      y = ~rating, 
      fill = ~season, 
      key := ~id) %>% 
  layer_points() %>%
  add_axis("x", title = "Airdate") %>%
  add_axis("y", title = "Rating") %>%
  add_legend("fill", title = "Season") %>%
  add_tooltip(all_values, "click")
4

2 回答 2

20

是的,这是可能的。通常客户端只发回实际在图中的数据列。要获取其他列,您应该使用键来索引原始数据:这是一个简单的可重现示例

library(ggvis)
mtc <- mtcars
mtc$id <- 1:nrow(mtc)

all_values <- function(x) {
  if(is.null(x)) return(NULL)
  row <- mtc[mtc$id == x$id, ]
  paste0(names(row), ": ", format(row), collapse = "<br />")
}

mtc %>% ggvis(x = ~wt, y = ~mpg, key := ~id) %>%
  layer_points() %>%
  add_tooltip(all_values, "hover")
于 2014-07-02T09:54:07.017 回答
2

一种解决方法是在key属性中传递 epid 变量,这通常是为了跟踪在转换过程中哪些观察结果相互对应,但它具有在数据中包含 epid 而不会产生任何副作用的预期效果:

breakingbad.episodes <- data.frame(firstaired.posix = as.POSIXct(c("2008-01-21 02:00:00", "2008-01-28 02:00:00")),
rating = c(87, 85), epid = as.factor(c(12,23)), season = as.factor(c(1,2)), somevar = c("special", "very_special"))

breakingbad.episodes %>% 
  ggvis(x = ~firstaired.posix, 
        y = ~rating, 
        fill = ~season, key := ~epid) %>% 
  layer_points() %>%
  add_axis("x", title = "Airdate") %>%
  add_axis("y", title = "Rating") %>%
  add_legend("fill", title = "Season") %>%
  add_tooltip(function(data){paste0("Rating: ", data$rating, "\n", "Epid: ", as.character(data$epid))}, "hover")

如果您需要使用原始数据集中的多个变量,您可以id为每一行添加一个具有唯一值的列,然后执行以下操作:

breakingbad.episodes <- data.frame(id = c(1,2), firstaired.posix = as.POSIXct(c("2008-01-21 02:00:00", "2008-01-28 02:00:00")),
rating = c(87, 85), epid = as.factor(c(12,23)), season = as.factor(c(1,2)), somevar = c("special", "very_special"))

breakingbad.episodes %>% 
  ggvis(x = ~firstaired.posix, 
        y = ~rating, 
        fill = ~season, key := ~id) %>% 
  layer_points() %>%
  add_axis("x", title = "Airdate") %>%
  add_axis("y", title = "Rating") %>%
  add_legend("fill", title = "Season") %>%
  add_tooltip(function(data){paste0("Rating: ", data$rating, "\n", "Epid: ",
as.character(breakingbad.episodes$epid[breakingbad.episodes$id == data$id]), "\n", 
"What this is: ", breakingbad.episodes$somevar[breakingbad.episodes$id == data$id])}, "hover")
于 2014-07-02T07:34:03.267 回答