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