在R中不显示带有plotly的折线图
我的最终目标是在同一个绘图上创建两个时间序列折线图,一个是静态的,另一个是动画的(前者指实际数据,后者指模型拟合值)。我正试图通过精心策划来实现这一目标,但我是一个全新的人,已经跨越了许多困难 为了在尝试上述操作之前先熟悉plotly,我最初尝试在plot上创建一个动画图形。然而,我甚至不能让表面上简单的脚本工作。运行下面的程序时,我的绘图区域上不会显示任何图形,就像没有数据一样。我的脚本是基于以下链接创建的:在R中不显示带有plotly的折线图,r,plotly,r-plotly,timeserieschart,R,Plotly,R Plotly,Timeserieschart,我的最终目标是在同一个绘图上创建两个时间序列折线图,一个是静态的,另一个是动画的(前者指实际数据,后者指模型拟合值)。我正试图通过精心策划来实现这一目标,但我是一个全新的人,已经跨越了许多困难 为了在尝试上述操作之前先熟悉plotly,我最初尝试在plot上创建一个动画图形。然而,我甚至不能让表面上简单的脚本工作。运行下面的程序时,我的绘图区域上不会显示任何图形,就像没有数据一样。我的脚本是基于以下链接创建的: 数据是一个53 obs、4个变量(日期、实际值、拟合、索引)的数据框架 单击动画的“
数据
是一个53 obs、4个变量(日期、实际值、拟合、索引)的数据框架
单击动画的“播放”按钮时,当动画的帧继续时,当在绘图区域上悬停时,数据点的工具提示会显示一段时间,但不会显示任何图形
提前感谢您的帮助,希望我为您提供了足够的信息。我错误地参与了脚本的一部分,下面是动画绘图的链接()。问题是我在使用前没有修改待框变量(在plot\ly
函数的frame
参数中使用的变量)
因此,为了使情节正常运作,我应该:1。通过函数2定义累加\u。将其与待框定变量一起用作输入,3。步骤2产生的输出列将是“plot_y”函数的帧参数值
初始工作数据框为data2
,其中列为ReqCreatedFull Date(作为POSIXct)、Requs\u Qnt\u pm(作为num)、Type(作为Factor)、Date(作为num)
,其中
日期=(年(重新创建的完整日期)+(月(重新创建的完整日期)-1)/12)
请参阅以下工作脚本:
library(plotly)
library(dplyr)
library(lubridate)
#step 1: function definition
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
#step 2: creation of to-be-used for framing variable
data2mod <- data2 %>%
accumulate_by(~date)
#graph creation
my_graph<-data2mod %>%
plot_ly(
x = ~date,
y = ~Requs_Qnt_pm,
split = ~Type,
frame = ~frame, #step 3, to be frame variable insertion
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F)
) %>%
layout(
xaxis = list(
title = "x axis title",
zeroline = F
),
yaxis = list(
title = "y axis title",
zeroline = F
)
) %>%
animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
) %>%
animation_slider(
hide = T
) %>%
animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
library(plotly)
图书馆(dplyr)
图书馆(lubridate)
#步骤1:函数定义
按%
动画按钮(
x=1,xanchor=“right”,y=0,yanchor=“bottom”
)
在xaxis和yaxis中showline=TRUE您能否只共享一个数据样本?您是否收到任何错误消息?我没有收到任何错误消息,绘图区域显示正确,动画滑块运行正常。无论如何,我发现了我的错误(检查下面的答案)。谢谢,太棒了!如果您在下面的答案中显示您所做的代码更改,以使其在@RK1的反馈中运行,这将非常有用。请检查我编辑的最终答案。
library(plotly)
library(dplyr)
library(lubridate)
#step 1: function definition
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
#step 2: creation of to-be-used for framing variable
data2mod <- data2 %>%
accumulate_by(~date)
#graph creation
my_graph<-data2mod %>%
plot_ly(
x = ~date,
y = ~Requs_Qnt_pm,
split = ~Type,
frame = ~frame, #step 3, to be frame variable insertion
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F)
) %>%
layout(
xaxis = list(
title = "x axis title",
zeroline = F
),
yaxis = list(
title = "y axis title",
zeroline = F
)
) %>%
animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
) %>%
animation_slider(
hide = T
) %>%
animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)