I am trying to create a bar chart using plotly in R. Bars must be sorted by value, and there are two classes of bars that I want to color differently. However, when I add color, the columns are split into two groups, sorted by groups. Any hint on how I can keep them in one group?
Here is my code:
plotting.df = data.frame(names=c("a", "b", "c", "d", "e", "f", "g"), value=c(1.9468656, 1.3867055, 1.0433950, 0.8949743, 0.3714826, 0.3605037, 0.3003954), label=c("y", "n", "y", "n", "n", "n", "n"), color=c("red", "black", "red", "black", "black", "black", "black")) plotting.df$names = factor(as.character(plotting.df$names), levels=as.character(plotting.df$names)[order(plotting.df$value, decreasing=TRUE)]) plotting.df = plotting.df[order(plotting.df$value, decreasing=TRUE), ] plot_ly(plotting.df, type="bar", x=names, y=value, name="Comp a", hoverinfo="text", text=c(paste("Name:", plotting.df$names, "<br>Value:", signif(plotting.df$value, digits=3), "<br>Label:", plotting.df$label)), color=color)
And an example:
