简体   繁体   English

如何更改水平 Plotly 条形图中轨迹的顺序?

[英]How can I change the order of traces in horizontal Plotly bar chart?

When I add a trace to a horizontal bar chart in R Plotly the additional trace is placed above the first trace, but in the legend it is vice versa.当我在 R Plotly 中向水平条形图添加轨迹时,附加轨迹位于第一条轨迹上方,但在图例中反之亦然。 How can I place the additional trece under the first one as in the legend?我怎样才能像传说中那样把额外的树放在第一个树下面?

And/or how can I change the order in the legend?和/或如何更改图例中的顺序?

条形图

library(plotly)
library(dplyr)
df <- mtcars %>% group_by(cyl) %>% summarise_at(c('qsec','mpg'), funs(mean))


figh <- plot_ly(df, y = ~ cyl, x = ~ qsec, type = 'bar', orientation = 'h'
               ,marker = list(color = 'rgb(80, 160, 200)')
)
figh <- figh %>% add_trace(x = ~ mpg
                         ,marker = list(color = 'rgb(220, 80, 80)')
)
figh

To change the order in the legend, you can change traceorder in legend using layout .要更改legend中的顺序,您可以使用layout更改traceorder中的跟踪顺序。

traceorder ... traceorder ...

Determines the order at which the legend items are displayed.确定图例项的显示顺序。 If "normal", the items are displayed top-to-bottom in the same order as the input data.如果为“normal”,项目将按照与输入数据相同的顺序从上到下显示。 If "reversed", the items are displayed in the opposite order as "normal".如果“反转”,项目将以与“正常”相反的顺序显示。

https://plotly.com/r/reference/layout/#layout-legend-traceorder https://plotly.com/r/reference/layout/#layout-legend-traceorder

layout(figh, legend = list(traceorder = "reversed"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM