r - how to plot dygraphs in multiple panels

橙三吉。 提交于 2019-11-29 22:57:57

问题


I was trying to plot dynamic graph in different panels as it could be done on the website using group such as

but it should be dynamic using dygraphs. An example code here:

library(quantmod)
library(dygraphs)
data(edhec)
R = edhec[, 1:4]
dygraph(R)

Many thanks in advance.


回答1:


Create multiple charts, using the synchronization feature here

To view it as one document, you will need to knit an HTML page. Look at this SO answer for details.

Your final result would be like this.




回答2:


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).

Here is working R code that produces grouped dygraphs candlestick plots:

# load packages
library(quantmod)
library(dygraphs)
library(htmltools)

# download time series into an environment
sym_bols <- c("VTI", "EEM")
data_env <- new.env()
quantmod::getSymbols(sym_bols, from="2017-01-01", env=data_env)

# create a list of dygraphs objects in a loop
dy_graph <- eapply(data_env, function(x_ts) {
  dygraphs::dygraph(x_ts[, 1:4], group="etfs",
    main=paste("Plot of:", substring(colnames(x_ts)[1], 1, 3)),
      width=600, height=400) %>% dygraphs::dyCandlestick()
})  # end eapply

# render the dygraphs objects using htmltools
htmltools::browsable(htmltools::tagList(dy_graph))

# perform same plotting as above using pipes syntax
# create a list of dygraphs objects in a loop
eapply(data_env, function(x_ts) {
  dygraphs::dygraph(x_ts[, 1:4], group="etfs",
    main=paste("Plot of:", substring(colnames(x_ts)[1], 1, 3)),
      width=600, height=400) %>% dygraphs::dyCandlestick()
}) %>% # end eapply
# render the dygraphs objects using htmltools
  htmltools::tagList() %>% htmltools::browsable()

The above R code produces the following grouped dygraphs candlestick plots:



来源:https://stackoverflow.com/questions/37267286/r-how-to-plot-dygraphs-in-multiple-panels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!