Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

r - Add data to ggvis tooltip that's contained in the input dataset but not directly in the vis

This is my input dataset:

> 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"     

For my ggvis, I'm using the following variables firstaired.posix and 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 ...

I successfully created my ggvis with a tooltip containing the rating information like this:

> 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")

But I actually want the tooltip to contain more data, like the epid variable, so I tried:

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

…Using as.character() because epid is an ordered factor – But the part of the tooltip is empty. (I also noticed the linebreak I intended to insert is missing, but that's a different problem).

It looks like the cause of this problem is that the vis object created by piping my dataset into ggvis doesn't contain the information I want to display, at least that's why I gathered by looking through the output of str() on the first example.

EDIT: I solved that linebreak issue, so there's no need to point me to ?add_tooltip – totally forgot about that.

EDIT: The accepted answer is working fine, even though it doesn't let me put arbitrary variables in the tooltip, it's pretty much what I need for my usecase, thanks! Here's what I did in the end:

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")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, it's possible. Normally the client only sends back the columns of data that are actually in the plot. To get other columns, you should use a key to index into the original data: This is simple reproducible example

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...