dygraph in R multiple plots at once
To plot multiple dygraphs in the same RStudio window you must first create a list of dygraphs objects, and then render the dygraphs list using package htmltools. Yihui Xie from RStudio provided the answer here:
Yihui Xie answer (but without grouping).
I answered a similar question here: my answer.
Here is working R
code that produces grouped (synchronized) dygraphs plots:
# create the time series
temperature <- ts(frequency = 12, start = c(1980, 1),
data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5,
25.2, 26.5, 23.3, 18.3, 13.9, 9.6))
rainfall <- ts(frequency = 12, start = c(1980, 1),
data = c(49.9, 71.5, 106.4, 129.2, 144.0, 176.0,
135.6, 148.5, 216.4, 194.1, 95.6, 54.4))
# create a list of dygraphs objects
library(dygraphs)
library(htmltools)
plotobj <- list(
dygraphs::dygraph(temperature, group="temp_rain", main="temperature"),
dygraphs::dygraph(rainfall, group="temp_rain", main="rainfall")
) # end list
# render the dygraphs objects using htmltools
htmltools::browsable(htmltools::tagList(plotobj))
The above R
code produces the following grouped (synchronized) dygraphs plots:
Dygraph with multiple series at different time intervals
You can use zoo::na.locf
function to fill the missing rows.
In your example:
stage <- zoo(sample(1:100, 154, replace=TRUE), seq(as.POSIXct("2018-08-23"), as.POSIXct("2018-08-31"), by = 4500))
rain <- zoo(sample(1:100, 154, replace=TRUE), seq(as.POSIXct("2018-08-23"), as.POSIXct("2018-08-31"), by = 54000))
eventData <- cbind(stage, rain)
head(eventData)
stage rain
2018-08-23 00:00:00 85 61
2018-08-23 01:15:00 71 NA
2018-08-23 02:30:00 10 NA
2018-08-23 03:45:00 16 NA
2018-08-23 05:00:00 31 NA
2018-08-23 06:15:00 92 NA
# fill NAs with na.locf
eventData <- na.locf(eventData)
head(eventData)
stage rain
2018-08-23 00:00:00 85 61
2018-08-23 01:15:00 71 61
2018-08-23 02:30:00 10 61
2018-08-23 03:45:00 16 61
2018-08-23 05:00:00 31 61
2018-08-23 06:15:00 92 61
This can be plotted the way you want it:
library(dygraphs)
dygraph(eventData, main = "Sitename") %>%
dyOptions(drawGrid = F) %>%
dyAxis("y", label = "Stage", independentTicks = TRUE) %>%
dyAxis("y2", label = "Rainfall ", independentTicks = TRUE) %>%
dySeries("stage", axis=('y')) %>%
dySeries("rain", axis=('y2'), stepPlot = T, fillGraph = T)
See also here for a deeper discussion about filling NAs.
Displaying multiple dygraphs on a grid in R-Markdown
I think I figured it out, not sure its the best solution, but adding a wrapper div with a display:inline-block;
property seems to work quite well.
I just added this line to the function that generates each dygraph:
htmltools::tags$div(theGraph, style = "padding:10px; width: 250px; border: solid; background-color:#e9e9e9; display:inline-block;")
so the updated code looks like this:
```{r graphs}
library(dygraphs)
library(htmltools)
makeGraphs = function(i){
theGraph <- dygraph(lungDeaths[, i], width = 400, height = 300, group = "lung-deaths")%>%
dyOptions(strokeWidth = 3) %>%
dyRangeSelector(height = 20)
htmltools::tags$div(theGraph, style = "padding:10px; width: 450px; border: solid; background-color:#e9e9e9; display:inline-block;")
}
lungDeaths <- cbind(mdeaths, fdeaths, ldeaths, mdeaths)
res <- lapply(1:4, makeGraphs )
htmltools::tagList(res)
```
Output Screenshot:
r - how to plot dygraphs in same panel
How about something like this...
library(dygraphs)
serie <- list(serie1, serie2, serie3)
full_serie <- do.call(cbind.data.frame, serie)
names(full_serie) <- c("serie1", "serie2", "serie3")
library(htmltools)
dy_graph <- dygraph(full_serie) %>%
dySeries("serie1") %>%
dySeries("serie2") %>%
dySeries("serie3")
htmltools::browsable(htmltools::tagList(dy_graph))
Related Topics
Automated Formula Construction
Using Variable Value as Column Name in Data.Frame or Cbind
R: Interpolation of Nas by Group
Let Ggplot2 Histogram Show Classwise Percentages on Y Axis
"Unpacking" a Factor List from a Data.Frame
Append Multiple CSV Files into One File Using R
How to Calculate Confidence Intervals for Nonlinear Least Squares in R
Convert Factor to Date Class for Multiple Columns
Plot Scatterplot on a Map in Shiny
Ggplot2 PDF Import in Adobe Illustrator Missing Font Adobepistd
How to Split Data Frame by Column Names in R
Joining Factor Levels of Two Columns
Robust and Clustered Standard Error in R for Probit and Logit Regression
Repeat the Re-Sampling Function for 1000 Times? Using Lapply
Combine Multiple PDF Plots into One File
How to Manage a Table/Matrix to Obtain Information Using Conditions
Large Integers in Data.Table. Grouping Results Different in 1.9.2 Compared to 1.8.10