r - 折线图未在 R 中以 plotly 显示
问题描述
我的最终目标是在同一个图上创建 2 个时间序列线图,一个是静态的,另一个是动画的(前者指的是实际数据,后者指的是我的模型的拟合值)。我正在尝试通过情节来实现这一目标,但是我是全新的并且已经克服了困难。
为了在尝试上述操作之前先熟悉 plotly,我最初尝试在绘图上创建一个动画图。但是,我什至无法使那个表面上简单的脚本工作。运行下面的图表时,我的绘图区域上不显示任何图表,就像没有数据一样。我的脚本是根据以下链接创建的:https ://plot.ly/r/cumulative-animations/
plot_ly(data
, x=~data$RequCreatedFull_Date
, y=~data$fitted_TotalRequ_Qnt_pm
, name="Fitted"
, type='scatter'
, mode = "lines"
, line = list(color = "rgb(255,128,0)")
, frame = ~data$RequCreatedFull_Date
, line = list(simplyfy = F)) %>%
layout(title="name"
, xaxis = list(range =
c(as.numeric(min(data$RequCreatedFull_Date))*1000
,as.numeric(max(data$RequCreatedFull_Date))*1000)
, type = "date"
, title = "Requisition Date"
, zeroline = F)
, yaxis = list(title="Total Requisition Qnts"
, range = c(1000,30000)
, zeroline = F)) %>%
animation_opts(frame = 100,
transition = 0,
redraw=FALSE) %>%
animation_button(x = 1, xanchor = "right", y = 0, yanchor = "bottom")
data
是一个 53 obs、4 个变量(日期、实际值、拟合、索引)数据框。
当单击动画的“播放”按钮并且动画的帧继续播放时,当将鼠标悬停在绘图区域上时,会显示数据点的工具提示片刻,但不显示图形。
预先感谢您的所有帮助,希望我为您提供了足够的信息。
解决方案
我错误地将脚本的一部分用于动画绘图的以下链接(https://plot.ly/r/cumulative-animations/)。问题是我在使用它之前没有修改要成帧的变量(用于函数frame
参数的plot_ly
变量)。
因此,为了使绘图正常工作,我应该:1. 定义accumulate_by
函数,2. 将它与要框化的变量一起用作输入,3. 步骤 2 产生的输出列将是 frame 参数的值'plot_ly' 函数。
初始工作数据框是data2
,列RequCreatedFull-Date(as POSIXct), Requs_Qnt_pm(as num), Type(as Factor), date(as num)
在哪里
date=(year(RequCreatedFull_Date)+(month(RequCreatedFull_Date)-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"
)
推荐阅读
- sqlite - 这些查询如何相互连接
- java - Java 和 Hibernate - 如何与 SuperMappedClass 而不是实体建立 OneToOne 关系
- java - 将 ComboBox 值设置为 null 后如何清除该字段?
- python - 硒错误(截屏时)“NoneType”对象没有属性“编码”
- sql - 如何在 SQL 选择查询中创建两个“where”条件
- angular - 从 Angular 7 中的 Observable 获取数据
- javascript - 如何使用处理程序动态添加下拉选择值?
- robotics - 任务计划实际上意味着什么?
- javascript - 数字字体大小由小变大
- python - 如何在不使用搅拌机的 bpy 的情况下创建一个用 Python 连接两个点的圆柱体?