我正在尝试使用R中的plotly生成条形图.条形图应按值排序,并且有两类条形图,我希望颜色不同.但是,当我添加颜色时,条形图分为两组,按组排序.有关如何将它们保存在一个组中的任何提示?
这是我的代码:
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, "
Value:", signif(plotting.df$value, digits=3), "
Label:", plotting.df$label)), color=color)
一个例子: