Filter legend in ggvis

35 views Asked by At

In this ggvis example, is there a way to filter the legend to reflect the input selection, e.g. only show "x" and "y" when "z" is not selected? This would be especially useful when the choices are extensive.

library(tidyverse)
library(ggvis)

data_df <- tibble(
  name = factor(c("x", "x", "x", "y", "y", "y", "z", "z", "z")), 
  quant = c(7, 8, 7, 8, 8, 8, 9, 9, 9), 
  year = factor(c(2014, 2015, 2016, 2014, 2015, 2016, 2014, 2015, 2016))
  )

data_df %>%
  ggvis(~ year, ~ quant, stroke = ~ name) %>%
  filter(name %in% eval(
    input_select(
      choices = levels(data_df$name),
      selected = c("x", "y"),
      selectize = TRUE,
      multiple = TRUE
    )
  )) %>%
  layer_lines()
1

There are 1 answers

0
tonyk On BEST ANSWER

I think this is what you would like?

library(tidyverse)
library(ggvis)

data_df <- tibble(
  name = c("x", "x", "x", "y", "y", "y", "z", "z", "z"), 
  quant = c(7, 8, 7, 8, 8, 8, 9, 9, 9), 
  year = factor(c(2014, 2015, 2016, 2014, 2015, 2016, 2014, 2015, 2016))
)

data_df %>%
  ggvis(~ year, ~ quant, stroke = ~ name) %>%
  filter(name %in% eval(
    input_select(
      choices = levels(as.factor(data_df$name)),
      selected = c("x", "y"),
      selectize = TRUE,
      multiple = TRUE
    )
  )) %>%
  layer_lines()