-
Notifications
You must be signed in to change notification settings - Fork 621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error bars displayed in incorrect order with color attribute assigned #762
Comments
I came across this too. One workaround is to add each sample group via add_trace. The code below demonstrates this. Error bars in the first plot (p1) are incorrectly assigned whereas error bars in the second (p2) are correct. library(plotly)
library(dplyr)
library(tidyr)
## Raw data
df <- data.frame(sample = rep(paste0('sample ', 1:5), 4),
x = rnorm(20),
group = rep(paste0('group ', 1:2), each = 10),
stringsAsFactors = FALSE
)
## Stats table
df2 <- df %>%
group_by(sample, group) %>%
summarise(avg = mean(x), sd = sd(x)) %>%
ungroup()
## Plotly barchart with error bars. Error bars are incorrectly assigned
p1 <- 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 errors
df3 <- df2 %>%
gather(key, value, -c(sample, group)) %>%
mutate(ref = paste0(group, ifelse(key == 'sd', '_sd', ''))) %>%
select(-group, -key) %>%
spread(ref, value)
## Plotly barchart displays error bars correctly
p2 <- plot_ly(df3, type = 'bar')
for (g in unique(df2$group)) {
p2 <- add_trace(p2, x = df3[['sample']], y = df3[[g]], name = g, error_y = list(array = df3[[paste0(g, '_sd')]]))
}
p2 |
Is this still the most viable solution for getting error bars to show correctly still? I have run into a similar situation with grouped time-series data. The y-error bars are not being associated with the correct bar. Attached is the data and below is the code I'm running: (will need to convert XLSX to csv) ptest <- plot_ly(nymphs, x = ~week, y = ~nymphs, type= "bar", color = ~treat, Below is a screenshot that I'm getting - the large error bars should be on "WAT 10", where the bars are much larger as well. Any help would be greatly appreciated - can't find documentation addressing this issue anywhere. |
I was running into this issue when I wanted to filter production data with crosstalk on manufacturing parameters/dates and label the scatterplot by one main parameter.
The resulting plots: The 1st plot is without labels for reference - the error bars are at the right position. |
Bump. Please fix this |
Bump again - this is v cumbersome workaround. |
When error_x is used along with a factor variable assigned to the color attribute in plot_ly, the resulting plot displays the error bars around the wrong points. Below is a working example using source code from the plotly for R book. You can see in the plot that the standard errors are being mapped to the wrong coefficients.
m <- lm(Sepal.Length ~ Sepal.Width * Petal.Length * Petal.Width, data = iris)
d <- broom::tidy(m) %>%
arrange(desc(estimate)) %>%
mutate(term = factor(term, levels = term),
one_col=cut(estimate,3,labels=c("Low","Medium","High")))
plot_ly(d, x = ~estimate, y = ~term,color=~one_col) %>%
add_markers(error_x = ~list(array=std.error,type="array")) %>%
layout(margin = list(l = 200))
The text was updated successfully, but these errors were encountered: