【问题标题】:Filter legend in ggvis在 ggvis 中过滤图例
【发布时间】:2018-07-12 02:05:26
【问题描述】:

在这个 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()

【问题讨论】:

    标签: r ggvis


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2017-02-02
      • 2015-04-02
      • 2015-08-04
      • 2015-07-14
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 2017-11-20
      相关资源
      最近更新 更多