Search code examples
rzoomingdygraphs

Zoom In dyGraphs in R and save image as png


I want to zoom in the dygraphs and save the image as png. I have plot the graph using dygraph library and saved it as png but it is not zoomed in version.

library("webshot")
library("htmlwidgets")
lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)
dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyHighlight(highlightCircleSize = 5, 
              highlightSeriesBackgroundAlpha = 0.2,
              hideOnMouseOut = FALSE)

saveWidget(ab, "/path/", selfcontained = TRUE, libdir = NULL)

basePng <- paste("images",paste(file,".png"), sep='/')
webshot::webshot("/path/",file=basePng)

enter image description here

But I want plot as below(zoomed in):

enter image description here

I want to plot zoomed in dygraph for more than 1000 files. Could anyone help me on this?


Solution

  • I hope this answer could help anyone,

    I use dyRangeSelector() to "zoom" on the graph, then webshotto save it.

    library(webshot)
    library(htmlwidgets)
    library(dygraphs)
    
    # webshot::install_phantomjs()
    
    lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)
    dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
            dyRangeSelector( dateWindow = c("1974-03-1","1974-06-10")) %>%
            dyHighlight(highlightCircleSize = 5, 
                  highlightSeriesBackgroundAlpha = 0.2,
                  hideOnMouseOut = FALSE)
    
    saveWidget(ab,file ="ab.html", selfcontained = TRUE, libdir = NULL)
    
    webshot(url = "ab.html",file="ab.png")