ggvis中的过滤图例

Filter legend in ggvis

在这个 ggvis 示例中,有没有办法过滤图例以反映输入选择,例如没有选择"z"时只显示"x"和"y"? 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()

我想这就是你想要的?

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