0

I want to use rangeslider() with ggplotly(). Unfortunately, defining the start and end of the slider does not work with ggplotly(). I'm using the rangeslider() in the server.R of an RShiny App. In the server it looks like the following code snippet:

output$maForwardVsSpotPLOT <- renderPlotly({
    plotFvsS <- MAForwardVsSpotMeanDifference(input$maStrategy, input$maTradingDays, input$maDateRange[1], 
                                              input$maDateRange[2], input$maPeriod)$FvsSMeanDiffPlot
    
    ggplotly(plotFvsS, dynamicTicks = TRUE) %>%
      rangeslider() %>%     # start = input$maDateRange[1], end = input$maDateRange[2]
      layout(hovermode = "x")
  
  })

In the UI I have:

tabItem(tabName = "maForwardVsSpot", 
        fluidRow(width = 7, dateRangeInput(inputId = "maFFDateRange", label = "Date Range", 
                                           start = "2018-01-01", end = Sys.Date()),
        fluidRow(width = 7, plotlyOutput(outputId = "maForwardVsSpotPLOT", width = "900px", 
                                         height = "600px")

plotFvsS is defined in an external function (MAForwardVsSpotMeanDifference) and looks like this:

plotFvsS  <- ggplot(dt.allDataFvsS, aes(x = date, y = meanDifference)) +
             coord_cartesian(ylim = c(-25, 25)) +   # Note: using ylim throws all data outside of ylim away!!! using coord_cartesian solves this problem
             ggtitle("Price Difference Forward vs. Spot") +
             geom_line(color = "blue", size = 0.9) + 
             labs(y = "EUR/MWh", x = "Date") +
             theme(axis.text = element_text(size = 9), axis.title = element_text(size = 5),
                   panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
                   panel.border = element_rect(fill = NA, color = "black", size = 1)) + 
             theme_bw(base_size = 17) +
             theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +
             geom_hline(yintercept = 0, color = "black") +
             geom_hline(yintercept = c(20,10,-10,-20), color = "#CCCCCC") +
             geom_vline(xintercept = c(12481, 12784, 13149, 13514, 13879, 14245, 14610, 14975, 15340, 15706, 
                                       16071, 16436, 16801, 17167, 17532, 17897, 18262, 18628)) +
             geom_vline(xintercept = c(12509, 12600, 12692, 12874, 12965, 13057, 13239, 13330, 13422, 13604, 
                                       13695, 13787, 13970, 14061, 14153, 14335, 14426, 14528, 14700, 14791, 
                                       14883, 15065, 15156, 15248, 15431, 15522, 15614, 15796, 15887, 15979, 
                                       16161, 16252, 16344, 16526, 16617, 16709, 16892, 16983, 17075, 17257, 
                                       17348, 17440, 17622, 17713, 17987, 18078, 18170, 18353, 18444, 18536), 
                        linetype = "dashed", color = "#666666")  +
             geom_vline(xintercept = 17805, linetype = "dashed", color = "red")

How can I add start and end in the rangeslider()?

Phil
  • 7,287
  • 3
  • 36
  • 66
MikiK
  • 398
  • 6
  • 19
  • 2
    Hi Michaela, it would be easier if you provide your ui and server code (relating to this particular plot, doesn't have to be the whole app) in order to help. – Phil Aug 31 '20 at 14:50
  • And please also provide some example data (by using `dput`) – starja Aug 31 '20 at 16:27
  • Hi @Phil ! I have edited the missing parts (UI, more information of the server.R). The plot works and is displayed almost correctly. The only thing that bothers me is the ```rangeslider()```, I would like it to start and end exactly where I or someone else choose the parameters as input. – MikiK Sep 01 '20 at 05:33
  • Welcome to stackoverflow! Unfortunately this is an [open issue](https://github.com/ropensci/plotly/issues/1551) with `ggplotly`. You could switch to `plot_ly()` until the bug is resolved. – ismirsehregal Sep 01 '20 at 09:41
  • Thank you for the information @ismirsehregal.. Unfortunately, the whole plot is created with ```ggplot()``` – MikiK Sep 01 '20 at 10:18
  • @ismirsehregal I've now tried to do it with ```plot_ly()``` and unfortunately I can't insert vertical lines like in the code above with ```geom_vline()```. Does anyone know anything about this? – MikiK Sep 01 '20 at 13:09
  • Please check [this](https://stackoverflow.com/questions/34093169/horizontal-vertical-line-in-plotly). Also feel free to create another question if you have issues to mimic the plot via `plot_ly`. – ismirsehregal Sep 01 '20 at 13:11

0 Answers0