Plotly charts can be enhanced with custom controls. One use of controls is to update an existing chart. This post will show how to add a couple of buttons to a chart, toggling error bars on and off. It should be noted that coloring by group affects the order (and hence validity) of error bars in a Plotly plot so a workaround has to be employed.
`summarise()` has grouped output by 'time'. You can override using the
`.groups` argument.
Initializing Chart and Adding Traces
We’ll build up the chart from the demo data by first initializing an empty plot as follows:
p <- plotly::plot_ly(type ='scatter', mode ='lines+markers')
The next step is to add a series of traces, one for each group, without error bars:
for (x inunique(d$group)) { p <- p |> plotly::add_trace(data = d |> dplyr::filter(group == x), visible =TRUE,x =~time, y =~mean, type ='scatter', mode ='lines+markers', color =~group)}
and then a second series of traces, this time with error bars but with the parameter visible=FALSE set to ensure that they are not visible when the chart is initially drawn:
for (x inunique(d$group)) { p <- p |> plotly::add_trace(data = d |> dplyr::filter(group == x), visible =FALSE,x =~time, y =~mean, type ='scatter', mode ='lines+markers', color =~group,error_y =~list(array = se, color = group))}
Since we have 5 groups, our plotly chart now contains 5 traces without error bars and 5 traces with error bars. It actually contains one additional empty trace corresponding to the initial plotly::plot_ly() call when the empty chart was built. This can be deduced using plotly::plotly_build() to observe the list object sent to plotly for plotting. At this stage the following code reveals a total of 11 traces:
Plotly does not have the option of a toggle switch or toggle button so we’ll add two buttons - one to plot without error bars and one to plot with error bars. The control works by changing the visible status so that only a subset of plots are visible. As mentioned above, we have 5 traces without error bars and 5 with. We also have the empty trace (the first trace), so in order to see just traces without error bars we’ll want to show only traces 2-6 and in order to see just traces with error bars we’ll want to show only traces 7-11. Here is the code to build the plotly menu buttons: