Plotly is a great plotting library that I’ve started to use extensively in part due to the ease in connecting graphs together under R. There is, however, a bug when using error bars in conjunction with a group variable - error bars are not drawn in the correct order. This can be fixed with a hack by adding each group as an individual trace.
# A tibble: 10 × 4
sample group avg sd
<chr> <chr> <dbl> <dbl>
1 sample 1 group 1 1.38 0.733
2 sample 1 group 2 1.23 0.344
3 sample 2 group 1 -1.04 0.649
4 sample 2 group 2 0.162 0.387
5 sample 3 group 1 -0.142 2.08
6 sample 3 group 2 0.272 0.284
7 sample 4 group 1 0.769 1.54
8 sample 4 group 2 -0.238 0.100
9 sample 5 group 1 0.715 0.568
10 sample 5 group 2 0.340 0.378
## Plotly barchart with error bars. Error bars are incorrectly assignedp1 <-plot_ly(df2, x =~sample, y =~avg, color =~group, type ='bar', error_y =list(array =~df2$sd))p1
## Create individual columns for group data and errorsdf3 <- df2 %>%gather(key, value, -c(sample, group)) %>%mutate(ref =paste0(group, ifelse(key =='sd', '_sd', ''))) %>%select(-group, -key) %>%spread(ref, value)df3