首页 > 解决方案 > 折线图未在 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 个变量(日期、实际值、拟合、索引)数据框。

当单击动画的“播放”按钮并且动画的帧继续播放时,当将鼠标悬停在绘图区域上时,会显示数据点的工具提示片刻,但不显示图形。

预先感谢您的所有帮助,希望我为您提供了足够的信息。

标签: rplotlyr-plotlytimeserieschart

解决方案


我错误地将脚本的一部分用于动画绘图的以下链接(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"
            )

推荐阅读