Search code examples
javascriptrmarkdownknitrdygraphs

Toggle logscale option for dygraph in rmarkdown


I would really like a dygraph that comes with a checkbox to toggle the Y axis to logscale. I know I can do this with shiny, but I can do pretty much everything else I need for this project using rmarkdown without shiny, which allows me to deliver .html without having to set up a shiny server.

I know it isn't complicated on the dygraph side of things http://dygraphs.com/options.html#logscale

And I think rmarkdown is certainly flexible enough to do it http://livefreeordichotomize.com/2017/01/24/custom-javascript-visualizations-in-rmarkdown/

But how?


Solution

  • You could use {.tabset} and plot both scales separately.

    ---
    output:
      html_document:
        code_folding: hide
        theme: flatly
        toc: yes
        toc_depth: 3
        toc_float: yes
    ---
    
    ```{r setup, include=FALSE}
    library(tidyr)
    library(dplyr)
    library(tibble)
    library(dygraphs)
    ```
    
    
    # Plots {.tabset .tabset-fade .tabset-pills}
    
    
    ## Normal Y
    
    ```{r plot_norm}
    dygraph(AirPassengers, main = "Airline Passengers / Month") %>%
      dyAxis("x", drawGrid = FALSE) %>%
      dyAxis("y", label = "Passengers (Thousands)") %>%
      dyOptions(includeZero = TRUE, 
                axisLineColor = "navy", 
                gridLineColor = "lightblue")
    ```
    
    
    ## Log Y
    
    ```{r plot_log}
    dygraph(AirPassengers, main = "Airline Passengers / Month") %>%
      dyAxis("x", drawGrid = FALSE) %>%
      dyAxis("y", label = "Passengers (Thousands)", logscale = TRUE) %>%
      dyOptions(logscale = TRUE,
                axisLineColor = "navy", 
                gridLineColor = "lightblue")
    ```