Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plotly parcoords (parallel coordinates) cannot plot a single line #2385

Open
MLopez-Ibanez opened this issue Aug 30, 2024 · 3 comments
Open

Comments

@MLopez-Ibanez
Copy link
Contributor

The following generates no plot, no warnings and no errors:

# Example from https://plotly.com/r/parallel-coordinates-plot/
library(plotly)
df <- read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/iris.csv")
df <- df[1L, , drop=FALSE] # plot one line only
fig <- df %>% plot_ly(type = 'parcoords',
          line = list(color = ~species_id,
                      colorscale = list(c(0,'red'),c(0.5,'green'),c(1,'blue'))),
          dimensions = list(
            list(range = c(2,4.5),
                 label = 'Sepal Width', values = ~sepal_width),
            list(range = c(4,8),
                 constraintrange = c(5,6),
                 label = 'Sepal Length', values = ~sepal_length),
            list(range = c(0,2.5),
                 label = 'Petal Width', values = ~petal_width),
            list(range = c(1,7),
                 label = 'Petal Length', values = ~petal_length)
            )
          )
fig
@trekonom
Copy link
Contributor

The issue is that with only row the single values get converted to a scalar instead of an array which is the datatype required by plotly.js. In general, plotly already accounts for that. Unfortunately this fails for parcoords traces (and I would guess that this is also true for other "special" trace types.)

As a workaround you could wrap in AsIs aka I():

library(plotly)

df <- read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/iris.csv")
df <- df[1, , drop = FALSE]

fig1 <- df %>%
  plot_ly(
    type = "parcoords",
    dimensions = list(
      list(
        range = c(2, 4.5),
        label = "Sepal Width",
        values = ~ I(sepal_width)
      ),
      list(
        label = "Sepal Length",
        values = ~ I(sepal_length)
      ),
      list(
        range = c(0, 2.5),
        label = "Petal Width",
        values = ~ I(petal_width)
      ),
      list(
        range = c(1, 7),
        label = "Petal Length",
        values = ~ I(petal_length)
      )
    )
  )
fig1

image

@trekonom
Copy link
Contributor

trekonom commented Aug 31, 2024

I just checked and the same issue arises with parcats traces:

library(plotly)

dd <- data.frame(
  from = "A",
  to = "B"
)

plot_ly(dd) %>%
  add_trace(
    type = "parcats",
    dimensions = list(
      list(values = ~ from),
      list(values = ~ to)
    )
  )

And can be fixed by wrapping in I():

plot_ly(dd) %>%
  add_trace(
    type = "parcats",
    dimensions = list(
      list(values = ~ I(from)),
      list(values = ~ I(to))
    )
  )

Created on 2024-08-31 with reprex v2.1.1

@trekonom
Copy link
Contributor

And also for splom traces:

library(plotly)

plot_ly(
  type = "splom",
  dimensions = list(
    list(values = 1, label = "A"),
    list(values = 2, label = "B")
  )
)

plot_ly(
  type = "splom",
  dimensions = list(
    list(values = I(1), label = "A"),
    list(values = I(2), label = "B")
  )
)

Created on 2024-08-31 with reprex v2.1.1

trekonom added a commit to trekonom/plotly.R that referenced this issue Aug 31, 2024
…e data_array propery of the "values" attribute. Closes plotly#2385.

* Add tests to check that "values" property has class "AsIs" for "parcoords", "parcats" and "splom" traces.

* Add NEWS entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants