在ggvis中动态选择组

时间:2015-11-24 16:29:43

标签: r ggvis

所以我试图使用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()

Plot 1

尝试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)

Plot 2

Plot 2: View 2

我仍然无法做对。我还尝试将add_legendlayer_linesstroke参数一起使用,看看我是否可以通过不同的颜色显示客户已被选中的图例,然后将图例显示为颜色和相应名称的一面,但根本不起作用。这对ggvis来说太过分了吗?或者我完全错过了什么?

1 个答案:

答案 0 :(得分:3)

试试这个:

vm.pagination

输出:

enter image description here

正如您在图片中看到的,您可以看到所有客户的图例,并且您可以看到所选择的三个客户(b,c和d)。该图将根据您的选择每次更改。我也使用 var vm = this; vm.posts = []; vm.pagination = { pageNumber: 0, pageSize: 9 }; vm.init = function () { load("next"); }; vm.load = function (direction) { // Use the argument and vm.pagination in conjunction to decide the result set // load posts from service }; 代替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) ,因为我发现它效果更好。