Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show/hide series interractively in dygraph

Is there a possibility to show/hide the series interactively in a R dygraph?

The idea is to compare many time series and have for example a ticking box where one could select/deselect the series to show or hide any of them.

I could not find such an option in the dygraphs package. Such ticking legend exist for example in the package leaflet with addLayersControl().

like image 825
Nikola Avatar asked Dec 07 '25 06:12

Nikola


1 Answers

You can do it with the package "shiny" in R Studio. No need to venture outside of R Studio (unless you really want to).

This chunk of code should work for anyone so long as you have the four packages installed. Use it as a jumping off point with your own data. I used drop down menus, but Shiny lets you use check boxes for user inputs as well. Hope this helps!

library(shiny)                # to dynamically select/plot variables here
library(tidyverse)            # required for transmute under server logic
library(dygraphs)             
library(magrittr)             # allows you to use pipes with dygraph()

a <- (1:10)                   # create example dataframe for dygraphs to plot
b <- rnorm(10, mean = 2)      
c <- rnorm(10, mean = 3)
d <- rnorm(10, mean = 4)

df <- as.data.frame(cbind(a,b,c,d))     

# User interface ----     # shiny does checkboxes too, modify accordingly
ui3 <- fluidPage(
  titlePanel("Example"),
  
  sidebarLayout(                                           # dropdown menus
    sidebarPanel(
      selectInput("column1", label = "Select a variable",  
              choices = c("None Selected", "Column B", "Column C", "Column D"), 
              selected = "Column B"),
      
      selectInput("column2", label = "Select a variable", 
              choices = c("None Selected", "Column B", "Column C", "Column D"), 
              selected = "None Selected")),
    
    mainPanel(dygraphOutput("dygraph"))
  )
)

# Server logic ----
server3 <- function(input, output) {
  output$dygraph <- renderDygraph({
    
  datax <- switch(input$column1,       # With no mapping for "None Selected"...
                   "Column B" = 2,     # ... nothing is graphed.
                   "Column C" = 3,
                   "Column D" = 4)
  
  datay <- switch(input$column2, 
                   "Column B" = 2,
                   "Column C" = 3,
                   "Column D" = 4)
  
  
  df2 <- df |>                            # df2 is dynamic, based on user input
    transmute(a, df[datax], df[datay])    # ... df2 passed to dygraph()
  
  dygraph(df2, main = "Fancy Plot Title") |>
    dyAxis("x", label = "Time") |>
    dyAxis("y", label = "Value")
  
})
  
}

# Run app ----
shinyApp(ui3, server3)           # you can run this in a browser if you prefer

like image 166
rob123 Avatar answered Dec 08 '25 18:12

rob123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!