2016-03-30 14 views
2

我有一個數據幀,其中包含時間和鍵值對,我想按鍵分組,然後爲每個數據組呈現一條平滑線。然後我想按鍵來標記它。我可以做所有,但基於密鑰獲取標籤。我無法看到在文檔中設置此位置。R - ggvis - 我如何標記多個平滑數據組

數據

  Time    newcol  newval 
23/03/2016 21:26 D1_ResponseTime_MS 55.44005444 
23/03/2016 21:27 D1_ResponseTime_MS 37.4510724 
23/03/2016 21:28 D1_ResponseTime_MS 5.286692372 
23/03/2016 21:29 D1_ResponseTime_MS 3.521776483 
23/03/2016 21:26 D2_ResponseTime_MS 2.444971186 
23/03/2016 21:27 D2_ResponseTime_MS 1.632372897 
23/03/2016 21:28 D2_ResponseTime_MS 4.772246899 
23/03/2016 21:29 D2_ResponseTime_MS 3.687779829 
23/03/2016 21:26 D3_ResponseTime_MS 0.752404455 
23/03/2016 21:27 D3_ResponseTime_MS 0.86613441 
23/03/2016 21:28 D3_ResponseTime_MS 0.663330605 
23/03/2016 21:29 D3_ResponseTime_MS 1.020344652 
23/03/2016 21:26 D4_ResponseTime_MS 10.62914139 
23/03/2016 21:27 D4_ResponseTime_MS 24.61302708 
23/03/2016 21:28 D4_ResponseTime_MS 17.00460387 
23/03/2016 21:29 D4_ResponseTime_MS 5.785255247 
23/03/2016 21:26 S1_ResponseTime_MS 12.82984893 
23/03/2016 21:27 S1_ResponseTime_MS 6.452076474 
23/03/2016 21:28 S1_ResponseTime_MS 1.763004864 
23/03/2016 21:29 S1_ResponseTime_MS 2.374506918 
23/03/2016 21:26 S2_ResponseTime_MS 2.034700375 
23/03/2016 21:27 S2_ResponseTime_MS 8.002695351 
23/03/2016 21:28 S2_ResponseTime_MS 25.60619709 
23/03/2016 21:29 S2_ResponseTime_MS 1.386355077 
23/03/2016 21:26 S4_ResponseTime_MS 3.443398856 
23/03/2016 21:27 S4_ResponseTime_MS 3.67701968 
23/03/2016 21:28 S4_ResponseTime_MS 7.901357583 
23/03/2016 21:29 S4_ResponseTime_MS 6.685758779 
23/03/2016 21:26 S5_ResponseTime_MS 1.007202665 
23/03/2016 21:27 S5_ResponseTime_MS 1.245214566 
23/03/2016 21:28 S5_ResponseTime_MS 1.167656668 
23/03/2016 21:29 S5_ResponseTime_MS 0.585119525 
23/03/2016 21:26 S6_ResponseTime_MS 1.913596402 
23/03/2016 21:27 S6_ResponseTime_MS 2.576953267 
23/03/2016 21:28 S6_ResponseTime_MS 1.908091138 
23/03/2016 21:29 S6_ResponseTime_MS 3.872218635 

結果

enter image description here

代碼

test5 %>% 
ggvis(~Time, ~newval) %>% 
group_by(newcol) %>% 
layer_smooths() %>% 
add_axis("x", properties = axis_props(
    axis = list(stroke = "black", strokeWidth = 2), 
    grid = list(stroke = "black"), 
    ticks = list(stroke = "black", strokeWidth = 2), 
    labels = list(angle = 45, align = "left", fontSize = 10) 
)) 
+0

請發表您的數據。例如,我們不能做任何事情截圖。 – mtoto

+0

更新了數據 –

+0

你可以在這裏得到dput()的輸出[link](https://onedrive.live.com/redir?resid=9DC28E4A63FAF571!5465&authkey=!ACXQVt-6MoXPUL8&ithint=file%2cout) –

回答

0

你可以嘗試添加stroke參數layer_smooths將由您的分組的顏色他們,同時也將增加一個傳說:

df %>% ggvis(~time, ~newval) %>% 
    group_by(newcol) %>% 
    layer_smooths(stroke=~newcol) %>% 
    add_axis("x", properties = axis_props(
     axis = list(stroke = "black", strokeWidth = 2), 
     grid = list(stroke = "black"), 
     ticks = list(stroke = "black", strokeWidth = 2), 
     labels = list(angle = 45, align = "left", fontSize = 10) 
    )) 

生產:

enter image description here

如果你希望每個標記線,您可以創建一個文本標籤一個小的數據幀和渲染,作爲一個附加的layer_text

label_df <- df %>% group_by(newcol) %>% sample_n(1) 

df %>% ggvis(~time, ~newval) %>% 
    group_by(newcol) %>% 
    layer_smooths(stroke=~newcol) %>% 
    layer_text(data=label_df, 
       text:=~newcol, 
       baseline:="middle", fontSize:=8) %>% 
    add_axis("x", properties = axis_props(
     axis = list(stroke = "black", strokeWidth = 2), 
     grid = list(stroke = "black"), 
     ticks = list(stroke = "black", strokeWidth = 2), 
     labels = list(angle = 45, align = "left", fontSize = 10) 
    )) 

enter image description here