plotly / plotly/plotly.R

Categoryorder not working when specifying plot type

Open
#2,082 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

When using the category order parameter, no plot is generated if the plot type is specified in the plot_ly call:

p <- plotly::plot_ly(type = 'bar')
colNames <- names(iris)
colNames <- colNames[-which(colNames == 'Species')]

for(trace in colNames){
  p <- p %>% plotly::add_trace(data= iris, x = ~ Species, y = as.formula(paste0("~`", trace, "`")), name = trace)
}

p  %>% layout(xaxis = list(categoryorder='total descending'))

With the following warning, which I don't understand since I'm not combining discrete and non-discrete data in the same axis:

Warning message:
Can't display both discrete & non-discrete data on same axis 

If I specify the type in add_trace instead of the plot, then it works correctly and the warning is gone:

p <- plotly::plot_ly()
colNames <- names(df)
colNames <- colNames[-which(colNames == 'Species')]

for(trace in colNames){
  p <- p %>% plotly::add_trace(type = 'bar', data= iris, x = ~ Species, y = as.formula(paste0("~`", trace, "`")), name = trace)
}

p %>% layout(xaxis = list(categoryorder='total descending'))

Maybe this is the intended behavior? If so, why does specifying the type in the traces works and not in the plot? The warning seems unclear to me.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied R reproduction with iris and compare plot_ly(type = 'bar') against add_trace(type = 'bar'). Trace how plot_ly and add_trace propagate the type and how layout(xaxis = list(categoryorder = 'total descending')) handles the resulting traces. Done means the reproduction no longer emits the warning and category ordering works consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.