R - ggvis - 我如何标记多个平滑的数据组

时间:2016-03-30 17:29:10

标签: r statistics 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)
 ))

1 个答案:

答案 0 :(得分: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