动态添加事件到 dygraph r [英] dynamically add events to dygraph r

查看:33
本文介绍了动态添加事件到 dygraph r的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中包含从异常值时间序列中选择的日期.我想将它们作为事件线动态添加到我的 dygraph 中.我的数据看起来像这样

I have a dataframe with selected dates from a time series that are outliers. I want to dynamically add them to my dygraph as event lines. My data looks like this

> head(data)
  Group Date
1 g1 2013-08-26
2 g1 2013-08-27
3 g2 2013-08-29
4 g2 2013-12-31
5 g3 2014-01-08

df_sub <- data[data$Group=='g1',]

我正在尝试创建一个函数,该函数接受组名并创建一个以异常值作为事件线的 dygraph.

I'm trying to create a function that takes in a group name and created a dygraph with outliers as event lines.

在我的例子中.对于 g1,有两个带有异常值的日期.

In my eg. for g1, there are two dates with outliers.

由于基本功能是这样的:

Since the basic function looks like this:

p <- dygraph(df_sub) %>% dyEvent(date = '2013-08-26', label='xyz', labelLoc='bottom')

我想动态传递两个日期并获取两个事件行.由于 dyEvent 只需要一个日期,有没有办法为多个日期执行此操作?

I want to dynamically pass two dates and get two event lines. As dyEvent only takes one date, is there a way to do this for multiple dates ?

谢谢

推荐答案

我发现你可以像这样构建 dygraph 图:

I figured out that you can build dygraph plots like this:

p <- dygraph(df_sub)
p <- p %>% dyEvent(date = '2013-08-26', label='xyz', labelLoc='bottom')

然后调用 p 调用绘图.因此,您可以使用 for 循环创建更多事件线:

Then calling p calls the plot. So you can create more event lines using for cycle:

p <- dygraph(df_sub)
for (i in 1:dim(df_sub)[1]){
p <- p %>% dyEvent(date = df_sub$Date[i], label='xyz', labelLoc='bottom')
}

这篇关于动态添加事件到 dygraph r的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆