【问题标题】:R - ggvis - how do i label mutiple smoothed data groupsR - ggvis - 我如何标记多个平滑数据组
【发布时间】:2016-03-30 17:29:09
【问题描述】:

我有一个包含时间和键值对的数据框,我想按键对其进行分组,然后为每个数据组呈现一条平滑线。然后我想按键标记它。除了根据密钥获取标签外,我什么都能做。我看不到在文档中的哪个位置进行设置。

数据

            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

结果

代码

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

【问题讨论】:

  • 请发布您的示例数据,我们无法对屏幕截图做任何事情。
  • 你可以在这里得到 dput() 的输出link
  • 打不开里面的东西,你试过layer_text()吗?
  • 在 mac 上以 sublime 文本打开没问题 - 或者使用任何文本编辑器打开。现在查看 layer_text。

标签: r statistics ggvis


【解决方案1】:

您可以尝试将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)
    ))

产生:

如果您确实想要标记每一行,您可以创建一个带有文本标签的小型数据框,并将其呈现为额外的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)
    ))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多