简体   繁体   中英

Line graph not displayed with plotly in R

My final aim is to create 2 time series line graphs on the same plot, with the one being static and the other being animated (the former refers to the actual data and the latter on my model's fitted values). I am trying to accomplish that with plotly, however I am completely new and have crossed difficulties.

In order to get familiar with plotly first before attempting the above I initially tried to create just one animated graph on a plot. However I cannot even make that ostensibly simple script work. When running the below no graph is displayed on my plot area, like there are no data. My script is created based on following link: 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 is a 53 obs, 4 variables (dates, actuals, fits, index) data frame.

When 'Play' button for animation is clicked and while the animation's frames proceed, when hovering on the plot area the data points' tooltips are displayed for a moment, however no graph is displayed.

Thank you in advance for all your assistance, hope I provided you with sufficient info.

I mistakenly took part of the script the below link for the animated plotting ( https://plot.ly/r/cumulative-animations/ ). The problem is that I did not modify the to-be-framed variable (variable to be used in frame parameter of plot_ly function) before using it.

Therefore, in order for the plot to work properly I should: 1. define accumulate_by function, 2. use it with the to-be-framed variable as input, 3. the output column produced from step 2 will be the value for the frame parameter of 'plot_ly' function.

Initial working data frame is data2 , with columns RequCreatedFull-Date(as POSIXct), Requs_Qnt_pm(as num), Type(as Factor), date(as num) where
date=(year(RequCreatedFull_Date)+(month(RequCreatedFull_Date)-1)/12) .

Please refer to working script below:

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"
            )

在xaxis和yaxis中showline = TRUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM