dyShading R dygraph

前端 未结 1 1089
無奈伤痛
無奈伤痛 2020-12-15 14:01

I am using the dygraphs package and I would like to add multiple shaded regions using the dyShading function. I would like not to have to specify m

相关标签:
1条回答
  • 2020-12-15 14:36

    For example:

    #create dygraph
    dg <- dygraph(nhtemp, main = "New Haven Temperatures")
    
    #add shades
    for( period in ok_periods ) {
      dg <- dyShading(dg, from = period$from , to = period$to )
    }
    
    #show graph
    dg
    

    If you have periods in a list:

    ok_periods <- list(
      list(from = "1920-1-1", to = "1930-1-1"),
      list(from = "1940-1-1", to = "1950-1-1"),
      list(from = "1960-1-1", to = "1970-1-1")
    )
    

    Using pipe

    If you want to use pipe, you could define a new function

    add_shades <- function(x, periods, ...) {
      for( period in periods ) {
        x <- dyShading(x, from = period$from , to = period$to, ... )
      }
      x
    }
    

    and use it in a chain:

    dygraph(nhtemp, main = "New Haven Temperatures") %>% 
      add_shades(ok_periods, color = "#FFFFCC" )
    
    0 讨论(0)
提交回复
热议问题