下面是一个使用 r 和 plotly 绘制堆积条形图的简单示例。 默认情况下,堆栈按字母顺序排列。 如何确定其他顺序?
library(plotly)
bars <- c("bar1", "bar1", "bar2", "bar2")
category <- c("(order1)small value", "(order2)big value", "(order1)small value", "(order2)big value")
#category <- c("(order2)small value", "(order1)big value", "(order2)small value", "(order1)big value")
count <- c(3, 6, 2, 7)
data <- data.frame(bars, category, count)
#View(data)
p <- plot_ly(data, x = ~bars, y = ~count, color = ~category) %>%
layout(width = 460, height = 230, barmode = 'stack' )
p
例如我希望能够确定该类别: “(order1)small value”在顶部。现在它位于底部,因为按字母顺序它位于其他类别之前。
非常感谢您
最佳答案
在创建数据之前添加这一行。使用 category
作为一个因素,您可以控制每个级别的顺序。
category <- factor(category, c("(order2)big value", "(order1)small value"))
关于r - 使用 r 和 plotly 确定堆叠条形图中的堆叠顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74008769/