Disappearing values in ggvis when using tooltip on

2020-04-14 03:58发布

问题:

This works fine

library(dplyr)
library(ggvis)

years <- as.factor(c(2013,2013,2014,2014,2015,2015))
months <- c(1,2,1,2,1,2)
values <- c(3,2,4,6,5,1)

df <- data.frame(years,months,values)


df %>% 
  group_by(years) %>% 
  ggvis(~months, ~values) %>% 
  layer_points( fill = ~years)

However, when I add a tooltip the points all appear momentarily but only the 2015 values remain

 df <- cbind(df, id = seq_len(nrow(df))) 

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

df %>% 
  group_by(years) %>% 
  ggvis(~months, ~values, key:= ~id) %>% 
  layer_points( fill = ~years) %>%
  add_tooltip(all_values, "hover") 

Probably some simple error, but any help appreciated cheers

回答1:

At the moment, trying to add a tooltip specific to a row when the data are grouped doesn't seem to work. This makes some sense, actually, as the grouping implies that you may want info by group.

You don't need grouping in your layer_points example at all, but you would need it if you wanted lines between the points for specific years as well as points. If this is what you wanted, you'd group the dataset after you add the points, and put key in layer_points instead of in the overall ggvis.

df %>% 
    ggvis(~months, ~values) %>% 
    layer_points( fill = ~years, key:= ~id) %>%
    add_tooltip(all_values, "hover") %>%
    group_by(years) %>%
    layer_lines(stroke = ~years, strokeWidth := 2)

This isn't totally ideal, because the tooltip still shows up for the lines even though lines don't have unique id data associated with them. To change this, make a small change to your tooltip function to check just if the id variable is NULL rather than the whole dataset from ggvis.

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

You can add a tooltip at the group level, but you'll need to figure out what sort of summary info you want to display. In the function for the tooltip you will be working with the grouping variable instead of id.

For example, you could just show all values associated with the group. The way I've set this up you'll need to click on the line to see the group info:

group_values1 = function(x) {
    if(is.null(x)) return(NULL)
    group = df[df$years == unique(x$years),]
    paste0(names(group), ": ", format(group), collapse = "<br />")
}

df %>% 
    ggvis(~months, ~values) %>% 
    layer_points( fill = ~years, key:= ~id) %>%
    add_tooltip(all_values, "hover") %>%
    group_by(years) %>%
    layer_lines(stroke = ~years, strokeWidth := 2) %>%
    add_tooltip(group_values1, "click")

You may want to show summary information, instead. In this example, I'll display the overall change in values and how many months passed in the tooltip. I make the summary dataset with functions from dplyr.

group_values2 = function(x) {
    if(is.null(x)) return(NULL)
    group = df[df$years == unique(x$years),]
    groupval = group %>% group_by(years) %>% 
        summarise(`Change in value` = max(values) - min(values), 
                            `Months Passed` = max(months) - min(months))
    paste0(names(groupval), ": ", format(groupval), collapse = "<br />")
}

df %>% 
    ggvis(~months, ~values) %>% 
    layer_points( fill = ~years, key:= ~id) %>%
    add_tooltip(all_values, "hover") %>%
    group_by(years) %>%
    layer_lines(stroke = ~years, strokeWidth := 2) %>%
    add_tooltip(group_values2, "click")


回答2:

The problem lies in you putting group_by before ggvis and calling add_tooltip afterwards. Just put group_by part after ggvis call

df %>% 
  ggvis(~months, ~values, key:= ~id) %>% 
  group_by(years) %>%
  layer_points( fill = ~years) %>%
  add_tooltip(all_values, "hover")

no idea why it happens though



标签: r ggvis