简体   繁体   English

在ggvis中过滤图例

[英]Filter legend in ggvis

In this ggvis example, is there a way to filter the legend to reflect the input selection, eg only show "x" and "y" when "z" is not selected? 在此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()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM