1

I am trying to plot time series financial OHLC data as candlesticks from R using Highstock JS. I am using rcharts package. However, the plot takes significant amount of time to display ( around 5 mins for 10,000 candles.

I have also tried using ggplot2 to construct candles and for interactivity, have used plotly. Though ggplot2 renders the plot very quick, when I use plotly, it slows down.

I basically require tooltip and horizontol scrollbar in my graph.

I have added a sample code created using ggplot2 and shiny with scroll

ui.R

library(shiny)

shinyUI(fluidPage(
  
  # Application title
  titlePanel(NULL),
  
  # Sidebar with a slider input for scrolling
  sidebarLayout(
    sidebarPanel(
      sliderInput("integer", "Scroll parameter:", 
                  min=51, max=2000, value=100)
    ),
    
    mainPanel(
      plotOutput("plot",
                 hover = hoverOpts(
                   id = "plot_hover")
      )
    )),
      
    
    fluidRow(
      column(width = 3,
             verbatimTextOutput("hover_info")
      )
  
)))

server.R

library(ggplot2)
library(shiny)
library(plotly)

shinyServer(function(input, output) {
  
  set.seed(1)
  s <- sample(seq(from = 1, to = 50, by = 1), size = 2000, replace = TRUE)
  data <- data.frame(x = 1:length(s), y = s)
  
  df <- reactive({
    
    df <- data[(input$integer - 50):input$integer,] # selecting only 50 rows of data frame and generating the plot
    return(df)
  })
  
  
  output$plot <- renderPlot({
    
    
    temp <- df()
    p <- ggplot(data = temp, mapping = aes(x=x,y=y))
    p <- p+geom_line()
    print(p)
   # g <- ggplotly(p) # for conversion to plotly
   # print(g)

  })
  
  output$hover_info <- renderPrint({
    cat("input$plot_hover:\n")
    str(input$plot_hover)
  })
})

the problem in this is that every time I change the scroll parameter, the plot is recreated and so the processing time is slow.( around .3 secs). Also the scroll is not smooth when I move with mouse.

Is there any way I can load the whole of ggplot in high res and have scrollbar .

Also I require a tooltip, which is why I cannot load as image I guess.

I understand that for interactivity, processing time required is high. But is there any other way of doing this for rendering faster charts.

I am running on MAC OS with 8 GB RAM.

Community
  • 1
  • 1
Mrudula
  • 11
  • 4
  • Can you provide a working JS Fiddle ? – Ahsan Dec 31 '15 at 08:54
  • Pardon me Ahsan, I am pretty new to this and do not know how to upload JS Fiddle. I am not able to get the export button in Highstock. I am using rcharts package in R which uses Highcharts API and the data is sent from there. Let me try to figure out how to upload the same in JS Fiddle and get back. – Mrudula Dec 31 '15 at 09:15
  • 2
    Hi @Mrudula, welcome to stackoverflow (SO)! I think Ahsan is asking for a minimal reproducible example to go along with your question. It's more likely that we will be able to help you if you provide something we can work from and use to show you how it might be possible to solve your problem. You can have a look at [this SO post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example in R. – Eric Fail Dec 31 '15 at 15:18
  • Thanks @EricFail for the suggestion. I have added a sample code now to understand my requirement – Mrudula Jan 04 '16 at 05:28
  • Why are 10k points taking so long for you to load in Highstock? Even with disabled dataGrouping a chart is running quite smooth - http://jsfiddle.net/kdkdso83/ – Kacper Madej Jan 14 '16 at 14:42
  • Thank you @KacperMadej. I do see its loading quite smooth in the example you provided. I cannot figure out why it slows down on mine when trying from R shiny.However for time being I have used ggplot2 and shiny as an alternative in a higher config system. I shall see the problem when I use highstock again – Mrudula Jan 28 '16 at 12:29

0 Answers0