0
我想繪製水平條並通過正值/負值對它們進行着色。問題是,即使我使用了categoryarray = cat
,酒吧的順序也發生了變化。如果我刪除color = sign
部分,訂單是好的。這是我到目前爲止的代碼:分類在垂直條形圖中的順序(在Plotly中)
library(plotly)
library(data.table)
dt <- data.table(cat = c("A", "B", "C", "D"),
val = c(12, -3, -9, 2))
dt[, sign := "p"]
dt[val < 0 , sign := "n"]
setorder(dt, -cat)
p <- plot_ly(data = dt, y = cat, x = val, type = "bar", orientation = "h", color = sign) %>%
layout(title = "Some title",
xaxis = list(categoryarray = cat, categoryorder = "array"),
showlegend = FALSE)
p