plotly / plotly/plotly.R

R plotly boxplots overwriting with numerical labels

Open
#2,274 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

R plotly boxplots overwriting with numerical labels

I've asked this question on SO here: https://stackoverflow.com/questions/76466189/r-plotly-boxplots-overwriting-with-numerical-labels, but I'm wondering if it's a bug with the latest versions of the plotly library in R

Plotting boxplots with numeric (or numeric-as-character) as x-variable results in boxplots a) not located in the correct locations and b) with boxes overplotted

library(plotly)

colors <- c("red","blue","green","pink","brown")
data.in <- data.frame(Age=25*rnorm(100)+50, IMD=sample(5,100,replace=TRUE))
fig <- plot_ly(data=data.in[data.in$IMD == "1",], y=~Age, type="box", color=I(colors[1]),name="1 - least")
fig <- fig %>% add_trace(data=data.in[data.in$IMD == "2",], y=~Age, type="box", color=I(colors[2]),name="2")
fig <- fig %>% add_trace(data=data.in[data.in$IMD == "3",], y=~Age, type="box", color=I(colors[3]),name="3")
fig <- fig %>% add_trace(data=data.in[data.in$IMD == "4",], y=~Age, type="box", color=I(colors[4]),name="4")
fig <- fig %>% add_trace(data=data.in[data.in$IMD == "5",], y=~Age, type="box", color=I(colors[5]),name="5 - most")
fig <- fig %>% layout(yaxis = list(title = "Age"),
                      xaxis = list(title = "IMD"),
                      boxgap = 0.1, boxgroupgap=0)

This results in a plot similar to
Screenshot 2023-06-13 at 15 41 15
Specifying 'x=~' in each add_trace does place the boxes in the correct places so is a temporary workaround, but I think that this may be a bug with the package, as the above code with labels which don't start with numbers (in the 'name=' attribute) does give the correct placement:

library(plotly)

colors <- c("red","blue","green","pink","brown")
data.in <- data.frame(Age=25*rnorm(100)+50, IMD=sample(5,100,replace=TRUE))
fig <- plot_ly(data=data.in[data.in$IMD == "1",], y=~Age, type="box", color=I(colors[1]),name="A")
fig <- fig %>% add_trace(data=data.in[data.in$IMD == "2",], y=~Age, type="box", color=I(colors[2]),name="B")
fig <- fig %>% add_trace(data=data.in[data.in$IMD == "3",], y=~Age, type="box", color=I(colors[3]),name="C")
fig <- fig %>% add_trace(data=data.in[data.in$IMD == "4",], y=~Age, type="box", color=I(colors[4]),name="D")
fig <- fig %>% add_trace(data=data.in[data.in$IMD == "5",], y=~Age, type="box", color=I(colors[5]),name="E")
fig <- fig %>% layout(yaxis = list(title = "Age"),
                      xaxis = list(title = "IMD"),
                      boxgap = 0.1, boxgroupgap=0)
Screenshot 2023-06-13 at 17 39 13

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 with the supplied R reproduction using plot_ly(), add_trace(), and layout(); compare numeric and nonnumeric trace names and the explicit x= workaround. Done means numeric or numeric-as-character boxplot traces occupy distinct intended x positions without requiring explicit x values. No source file or test is named in the issue.

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
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.