在ggvis中过滤图例

时间:2018-02-01 16:56:46

标签: r ggvis

在这个ggvis示例中,有没有办法过滤图例以反映输入选择,例如只显示" x"和" y"什么时候" z"没有被选中?当选择范围很广时,这将特别有用。

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 个答案:

答案 0 :(得分:1)

我认为这是你想要的?

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