首页 文章

线图在R中没有用图表显示

提问于
浏览
0

我的最终目标是在同一个图上创建2个时间序列线图,其中一个是静态的,另一个是动画的(前者指实际数据,后者指我模型的拟合值) . 我试图用阴谋来实现这一点,但是我是全新的并且遇到了困难 .

为了在尝试上述之前首先熟悉情节,我最初尝试在一个情节上创建一个动画图形 . 但是,我甚至无法使表面上简单的脚本工作 . 当运行以下时,我的绘图区域上没有显示图表,就像没有数据一样 . 我的脚本基于以下链接创建: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个变量(日期,实际值,拟合,索引)数据框 .

单击动画的“播放”按钮时,动画的帧继续进行,当悬停在绘图区域时,数据点的工具提示会显示片刻,但不显示图形 .

提前感谢您的帮助,希望我能为您提供足够的信息 .

2 回答

  • 1

    我错误地将以下链接的部分脚本用于动画绘图(https://plot.ly/r/cumulative-animations/) . 问题是在使用之前我没有修改要成框的变量(在 plot_ly 函数的 frame 参数中使用的变量) .

    因此,为了使绘图正常工作,我应该:1 . 定义 accumulate_by 函数,2 . 将它与待框架变量一起用作输入,3 . 从步骤2生成的输出列将是框架的值'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"
                )
    
  • 0

    在xaxis和yaxis中showline = TRUE

相关问题