I am trying to adapt this example in Plotly's documentation, in order to show the percentage of answers (Yes/No) to a question, for two classes of people. For example, input data could be:
x1 <- c(62.7, 89.2) # answered yes; can change, as values come from another function
x2 <- sapply(x1, function(x) 100-x) # answered no
y <- c("Class1", "Class2")
data <- data.frame(y, x1, x2)
library(plotly)
# ...generate chart according to example Plotly's in documentation
Which would result in something like this. However, the order of the bars is different from the way they were intended to be displayed, given the order in which data is arranged in the vectors y, x1 and x2.
This question mentions a similar problem with bar charts in Plotly, but none of the possible workarounds suggested there seems to make any difference here - I am not sure if it has something to do with the fact that those were vertical, non-stacked bar charts. Besides, I am searching for a solution that do not require the values of x1 and x2 to be known beforehand, as the intention is to generate these charts dynamically.