对覆盖在烛台图上的 Plotly 条形图数据进行排序

Sorting Plotly bar chart data overlaid on candlestick chart

我在烛台图上叠加条形图,所有数据均引用自同一数据框。我想对条形图数据的数据进行升序排序,但找不到任何指南来帮助进行叠加。 有没有人让这个工作?我已附上当前输出,但希望对灰色条进行排序。

O <- round(runif(62,-10,10),2)
H <- round(runif(62,-10,10),2)
L <- round(runif(62,-10,10),2)
C <- round(runif(62,-10,10),2)
LMA <- round(runif(62,-10,8),2)
OHLC <- data.frame(tick,O,H,L,C, LMA)
OHLC$ticker <- factor(OHLC$ticker, levels=unique(OHLC$ticker)[order(data$LMA)])
xform <- arrange(OHLC, LMA)

p<- plot_ly(xform, x=~tick, type="candlestick",
             open= ~O, close=~C, high=~H, low=~L) %>% 
  add_bars(x=~tick, y=LMA, marker = list(color = 'rgb(204,204,204)'))%>%
  add_lines(y=4, line = list(dash='dash'))%>%
  add_lines(y=-4, line = list(color='rgb(22, 96, 167)', dash='dash'))%>%
  layout(title="Title", showlegend=F)
p

dput(xform)
structure(list(tick = c("MBFI", "Y", "EV", "PACW", "NAVI", "NYCB", 
"RGA", "ASB", "DCT", "SEIC", "TCF", "WTFC", "MCY", "SBNY", "CATY", 
"KMPR", "FII", "PRI", "FHN", "CHFC", "TCBI", "WRB", "ORI", "PNFP", 
"SF", "SNV", "FAF", "TREE", "THG", "VLY", "WAFD", "FR", "PB", 
"FDS", "BOH", "BXS", "CFR", "IBKR", "IBOC", "FNB", "MKTX", "LM", 
"TRMK", "GNW", "WBS", "UMPQ", "BRO", "UBSI", "AFG", "EVR", "OZRK", 
"AHL", "PRSP", "CBSH", "CNO", "SLM", "STL", "FULT", "HOMB", "RNR", 
"UMBF", "EWBC"), O = c(-6.74, 8.06, -9.68, -6.02, 1.15, 2.91, 
-3.76, 6.24, -8.42, 6.98, 9.73, 8.88, -3.06, -8.61, -3.92, 8.47, 
-7.84, -5.91, -8.45, 1.37, -9.74, 4.26, 6.71, -7.17, 3.95, -9.01, 
1.56, -1.38, 4.01, -6.98, 2.77, 8.71, 3.76, 1.6, -3.32, -3.87, 
6.3, -9.02, 5.39, -1.37, -2.11, -8.3, 6.65, -5.33, -3.79, 8.63, 
-2.87, 7.57, 7.9, -0.27, 1.28, -7.68, -9.69, 2.35, 4.01, 2.36, 
-4.37, -1.54, -8.56, 0.98, -3.26, 7.26), H = c(-8.06, -1.24, 
-7.93, 7.3, 0.05, -8.75, -4.05, -6.87, -2.35, 0.34, -6.75, 2.6, 
-9.63, -1.82, 6.29, 0.79, 0.43, 6.23, -6.79, -9.23, 6.37, -3.2, 
-7.05, 1.14, -4.31, -1.04, 9.04, -4.33, -5.55, -7.21, 0.24, -3.1, 
-2.07, 1.07, -5.97, 5.27, 9.74, -3.03, -1.45, 0.34, -5.39, -9.1, 
6.12, 2.12, 2.01, 6.12, 0.7, -6.72, 2.54, -1.18, 1.42, -4.01, 
-1.03, -0.49, -8.09, 1.69, -3.35, 8.74, -7.29, 1.1, 6.4, -8.89
), L = c(-9.68, 4.91, -9.11, -4.65, 3.56, -4.67, -2.59, 0.66, 
-2.05, 9.44, 3.67, 6.25, 9.35, -9.31, -0.23, 9.56, 2, -9.9, 5.99, 
9.16, -0.79, 5.7, 2.22, -4.43, -9.53, -8.52, -0.23, -1.95, 8.29, 
-8.29, -9.25, 7.04, -5.39, -5.61, 5.6, 2.38, 8.72, -5.07, -4.11, 
-5.29, -4.7, 6.15, -3.52, -8.48, -9.06, 7.48, -0.06, 7.6, -1.2, 
-8.02, -3.91, 2.26, -6.21, -2.31, 5.74, 9.98, 4.91, -6.14, -6.66, 
7.31, -0.3, -5.72), C = c(-8.51, -3.3, -5.39, -0.09, -3.18, 9.1, 
-5.29, 0.39, -9.34, 1.79, -3.09, -2.33, -7.48, 7.39, -5.85, -2.95, 
-8.67, 8.61, 8.96, -1.02, -1.45, 5.18, -4.78, 5.41, 2.25, -0.26, 
1.04, 2.46, -3.95, -3.47, -8.46, 4.97, 8.7, 7.41, -4.07, -8.53, 
6.84, -8.9, 4.6, 4.07, 0.71, 4.02, 0.26, -9.55, -7.3, 4.33, -3.46, 
-1.55, -4.91, -7.87, 5.14, 2.56, 9.15, 1.07, 2.44, -2.75, 0.72, 
-9.93, -1.9, -4.03, -5.99, -2.23), LMA = c(-9.49, -9.22, -9.21, 
-9.19, -8.75, -8.49, -8.46, -7.99, -7.95, -7.86, -7.32, -7.09, 
-7, -5.96, -5.83, -5.77, -5.07, -4.78, -4.67, -4.62, -4.44, -4.18, 
-4.05, -3.06, -2.76, -2.72, -1.79, -1.03, -0.95, -0.85, -0.45, 
0, 0.14, 0.18, 0.53, 0.77, 0.98, 1.39, 2.32, 2.36, 2.48, 2.51, 
2.64, 2.95, 3.2, 3.25, 3.26, 3.61, 4.2, 4.29, 4.39, 4.45, 4.69, 
5.46, 5.72, 5.72, 5.78, 6.26, 6.34, 6.59, 6.83, 7.18)), class = "data.frame", row.names = c(NA, 
-62L))

可以使用 categoryorder 控制 x 轴上的类别顺序,如下所示:

p <- plot_ly(xform, x=~tick, type="candlestick",
             open= ~O, close=~C, high=~H, low=~L) %>% 
  add_bars(x=~tick, y=~LMA, marker=list(color='rgb(204,204,204)')) %>%
  add_lines(y=4, line=list(dash='dash')) %>%
  add_lines(y=-4, line=list(color='rgb(22, 96, 167)', dash='dash')) %>%
  layout(title="Title", showlegend=F, xaxis=list(categoryorder="trace"))
p

可能的选项是 tracecategory ascendingcategory descendingarray,如 here 所述。