如何更改水平 Plotly 条形图中轨迹的顺序?
How can I change the order of traces in horizontal Plotly bar chart?
当我在 R Plotly 中向水平条形图添加轨迹时,附加轨迹位于第一条轨迹上方,但在图例中反之亦然。我怎样才能像传说中那样把额外的树放在第一个树下面?
And/or如何更改图例中的顺序?
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
要更改图例中的顺序,您可以使用 layout
更改 legend
中的 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. If "reversed", the items are displayed in the opposite
order as "normal".
https://plotly.com/r/reference/layout/#layout-legend-traceorder
layout(figh, legend = list(traceorder = "reversed"))
当我在 R Plotly 中向水平条形图添加轨迹时,附加轨迹位于第一条轨迹上方,但在图例中反之亦然。我怎样才能像传说中那样把额外的树放在第一个树下面?
And/or如何更改图例中的顺序?
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
要更改图例中的顺序,您可以使用 layout
更改 legend
中的 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. If "reversed", the items are displayed in the opposite order as "normal".
https://plotly.com/r/reference/layout/#layout-legend-traceorder
layout(figh, legend = list(traceorder = "reversed"))