rr-dygraphs

Why are the colors changing on my graph when I remove the grid lines?


I have the following graph:

library(dygraphs)


set.seed(1)
date <- seq(as.Date("2010-01-01"), as.Date("2010-12-01"), by = "month")
var1 <- rnorm(length(date), mean = 100, sd = 10)
var2 <- rnorm(length(date), mean = 50, sd = 5)
df <- data.frame(date, var1, var2)

library(xts)
df_xts <- xts(df[,-1], order.by = df$date)

dygraph(df_xts, main = "Stacked Graph") %>%
    dySeries("var1", label = "Var1") %>%
    dySeries("var2", label = "Var2") %>%
    dyOptions(stackedGraph = TRUE) %>%
    dyRangeSelector(height = 20)

enter image description here

When I remove the grid lines, the colors seem to change:

dygraph(df_xts, main = "Stacked Graph") %>%
    dySeries("var1", label = "Var1") %>%
    dySeries("var2", label = "Var2") %>%
    dyOptions(stackedGraph = TRUE) %>%
    dyRangeSelector(height = 20) %>%
    dyOptions(fillGraph = TRUE, drawGrid = FALSE) 

enter image description here

Why is this happening and what can I do to fix this (i.e. make the colors in the second graph appear more similar to the first graph)?

Note: Here is the answer that worked for me based on @user20650's comment:

dygraph(df_xts, main = "Stacked Graph") %>%
    dySeries("var1", label = "Var1") %>%
    dySeries("var2", label = "Var2") %>%
    dyOptions(stackedGraph = TRUE, drawGrid = FALSE) 

Solution

  • Your use of multiple dyOptions calls is likely causing some options to be over-written. Soln: just use one dyOptions call.

    Example:

    library(xts)
    library(dygraphs)
    
    # reproducible data
    data(sample_matrix)
    sample.xts <- as.xts(sample_matrix[, 1:2])
    
    dygraph(sample.xts) |>
        dyOptions(stackedGraph = TRUE, drawGrid = FALSE) |>
        dyRangeSelector(height = 20) 
    

    Toggle the drawGrid = FALSE option to get the same plot with/without grid lines.

    enter image description here