Sometimes you are lucky ... I worked on the same thing a few weeks ago, and I found that the documentation is not entirely clear how to do this. But you were pretty close.
How to do it - step by step:
- You need to install a plotter for each dyseries
- The
plotter argument in the dyseries command does not accept function names. But it should be a javascript function as plain text. - Stacking bars is easier. Multi-bars need a way to pass an argument to a javascript function that you cannot do directly in the package. So I had to make a workaround (at least I did not find a better way to do this in R).
BTW, setting the dyPlotter command dyPlotter not work, because it sets the plotter globally for all dySeries on the chart. At least that's what I think.
So, without much ado, here is my code. I added some more test data to show all the features.
Test data:
library(xts) library(dygraphs) test<-xts(matrix(rnorm(100*4), ncol=4, nrow=100), order.by=seq.POSIXt(as.POSIXct("2017-01-01 00:00", tz="UTC"),by=3600, length.out = 100)) colnames(test)<-c("Series_A","Series_B", "Series_C", "Series_D")
Functions:
dy_position<-function(data_final, plot_title, y2_names=NULL, y1_label, y2_label, y1_step=F, y2_step=F, stacked=T){ data_final<-reorder_xts(data_final, y2_names)
Some Explanation:
dy_position runs the entire chart. It uses separate plotters for each series.
reorder_xts requires all line graphs to be on the right end of xts. This is necessary for a multi-byte plot. Since the java script loops through all the series (sets) in order to determine the width of the bars, and we need to make sure that we do not iterate over the lines, which are string charts. Otherwise, we have additional bars.
multibar_combi_plotter does just that. It takes a numerical parameter lines_names and modifies the javascript line so that it intersects all the charts except line_name (i.e. the last series on the right side of the xts). Note the few small %s in the line for the sprintf command! Later, he returns as a plotter character for dySeries argument.
All javascript code is taken directly from the examples in the dygraphs folder.
Here are some examples ...
<strong> Examples:
dy_position(test,plot_title = "Test1", y2_names = c("Series_C","Series_D"),y1_label = "Axis1", y2_label = "Axis2", stacked=F) dy_position(test,plot_title = "Test1", y2_names = c("Series_C","Series_D"),y1_label = "Axis1", y2_label = "Axis2", stacked=T) dy_position(test,plot_title = "Test1", y2_names = c("Series_B","Series_C","Series_D"),y1_label = "Axis1", y2_label = "Axis2", stacked=T) dy_position(test,plot_title = "Test1", y2_names = c("Series_D"),y1_label = "Axis1", y2_label = "Axis2", stacked=F) dy_position(test,plot_title = "Test1", y2_names = c("Series_D"),y1_label = "Axis1", y2_label = "Axis2", stacked=T) dy_position(test,plot_title = "Test1", y2_names = NULL ,y1_label = "Axis1", y2_label = "Axis2", stacked=F) dy_position(test,plot_title = "Test1", y2_names = NULL ,y1_label = "Axis1", y2_label = "Axis2", stacked=T)