这是我的输入数据集:
> 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.posix
和rating
:
> 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")
答案 0 :(得分: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")
答案 1 :(得分: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")