plotly / plotly/plotly.R

Adding 'name' parameter to plot_ly() function removes factor ordering.

Open
#1,064 3 comments 1 reaction 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

We sometimes use the 'name' parameter within the plot_ly() function to add a suffix to the values shown in a legend. Values in the legend are shown because a variable (legend variable) is used for the 'color' parameter in plot_ly().

Since a few days/weeks, using this method to add a suffix leads to the ordering of 'legend variable' to switch to alphabetical order instead of its original factor levels.

I added an example below.

library(plotly)
library(lubridate)

factor <- factor(x = c('bbb', 'ccc', 'aaa'), levels = c('bbb', 'ccc', 'aaa'))
number <- c(1,2,3)
date <- c(ymd(20170101), ymd(20170202), ymd(20170303))

levels(factor)

df <- data.frame(letter = factor, number = number, date = date)

levels(df$letter)

plot_ly(df, x = ~number, y = ~date, type = 'bar', color = ~letter)
plot_ly(df, x = ~number, y = ~date, type = 'bar', color = ~letter, name = 'ff')
plot_ly(df, x = ~number, y = ~date, type = 'bar', color = ~letter, name = c('asd', 'ff', 'ss'))

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 reproducible R examples using plot_ly() with color = ~letter, first without name and then with each name form. Trace the plot_ly() handling of factor levels and legend names; done means the legend preserves the factor order bbb, ccc, aaa in all shown cases.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.