Disappearing values in ggvis when using tooltip on grouped data

后端 未结 2 340
臣服心动
臣服心动 2021-01-23 05:14

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)

d         


        
2条回答
  •  北海茫月
    2021-01-23 05:53

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

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

提交回复
热议问题