【问题标题】:Dynamically selecting groups in ggvis在 ggvis 中动态选择组
【发布时间】:2015-11-24 16:29:43
【问题描述】:

所以我尝试使用ggvis 可视化以下数据,因为我希望能够查看不同的客户和不同的客户组合。我想使用线图,然后能够选择两个或三个并在图上同时查看它们。问题是我无法准确判断我正在查看哪些内容。每次我尝试一些不同的东西;我遇到了别的东西。见下文,数据被称为m3

customer   score    model
a          0.437    1
a          0.471    2
a          0.036    3
b          0.455    1
b          0.371    2
b          0.462    3
c          0.280    1
c          0.042    2
c          0.279    3
d          0.282    1
d          0.470    2
d          0.246    3
e          0.469    1
e          0.500    2
e          0.303    3
f          0.290    1
f          0.387    2
f          0.161    3
g          0.075    1
g          0.111    2
g          0.116    3

尝试 1: 这样,我可以看到线条,但如果我选择了两个客户,我会收到一个奇怪的警告,并且我无法确定哪些线条属于谁。它还为两个客户放弃了第二个model 观察。

m3 %>% ggvis(x=~factor(model),y=~score)%>% 
  filter(customer == eval(input_select(choices = as.character(m3$customer),multiple=TRUE,label='Select which Domains to view'))) %>% 
  layer_lines()

尝试 2: 现在我可以看到发生了什么。不过还是不对。第二张照片只是选择了“a”和“c”。

m3 %>% ggvis(x=~factor(model),y=~score)%>% 
  filter(customer == eval(input_select(choices = as.character(m3$customer),multiple=TRUE,label='Select which Domains to view'))) %>% 
  layer_lines() %>% layer_text(text:= ~customer)

我还是没弄好。我还尝试使用add_legendlayer_linesstroke 参数来查看我是否可以获得图例来显示已选择不同颜色的客户,然后将图例与颜色放在一边相应的名称,但这根本不起作用。这对ggvis 来说太过分了吗?还是我完全错过了什么?

【问题讨论】:

    标签: r ggvis


    【解决方案1】:

    试试这个:

    m3 %>% 
          #group by customer in order to separate them
          group_by(customer) %>% 
          #the normal ggvis call
          ggvis(x=~factor(model),y=~score) %>%
          #filter in the same way that you did 
          #but add unique in order to pick one customer 
          #also make sure you use %in% instead of == in order to 
          #select multiple customers. == was the reason for the warning you received
          #in your code
          filter(customer %in% eval(input_select(choices = unique(as.character(m3$customer)),
                                                 multiple=TRUE,
                                                 label='Select which Domains to view'))) %>%
          #add layer_paths with the stroke argument in order for
          #different customers to have different colours (and a legend)
          layer_paths(stroke = ~customer)
    

    输出:

    正如您在图片中看到的,您有一个包含所有客户的图例,并且您可以看到绘制的三个选择的客户(b、c 和 d)。情节将根据您的选择每次更改。我也使用layer_paths 而不是layer_lines,因为我发现它效果更好。

    【讨论】:

    • 而且,如果 OP 没有注意到您的更改,在选择多个值时,在 filter 中使用 %in% 而不是 == 非常重要。
    • 哎呀感谢@aosmith你是对的,我忘了提。我将在代码中的 cmets 中突出显示它,因为它确实非常重要
    • 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊现在说的太有道理了!谢谢!这太棒了!
    • 你非常欢迎@Hillary。很高兴我能帮上忙:)
    猜你喜欢
    • 2020-04-11
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2014-12-29
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    相关资源
    最近更新 更多