2015-11-24 57 views
4

因此,我試圖使用ggvis將以下數據可視化,因爲我希望能夠查看不同的客戶和不同的客戶組合。我想使用一條線圖,然後可以選擇兩個或三個並在圖上同時查看它們。這個問題是我不能確切地告訴我正在查看哪些。每次我嘗試一些有點不同的東西時,我碰到別的東西。見下文,數據稱爲m3在ggvis中動態選擇組

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:有了這個,我可以看到線條,但我得到一個奇怪的警告,如果我選擇了兩個客戶,我真的不能告訴哪些行屬於誰。同時它也降低了兩個客戶的第二個觀察結果。

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來說太過分了嗎?或者我完全錯過了什麼?

回答

3

試試這個:

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) 

輸出:

enter image description here

正如你可以在圖片中看到你與所有的客戶一個傳奇,你可以看到選擇的三個客戶( b,c和d)繪製。根據您的選擇,情節每次都會改變。我還使用layer_paths而不是layer_lines,因爲我發現它效果更好。

+2

而且,如果OP沒有注意到您的更改,則在選擇多個值時,在過濾器中使用'%in'而不是'=='是非常重要的。 – aosmith

+0

哎呀謝謝@aosmith你是對的,我忘了提及它。我會在代碼中的評論中強調它,因爲它確實非常重要 – LyzandeR

+0

Ahhhh現在非常有意義!謝謝!這太棒了! – Hillary