Skip to content

ColorScale Will Not Change If Specifying with colorscale = c('hexcode1', 'hexcode2') #1846

Description

@OhStan

Hi,

After following the guide here, https://plotly.com/r/3d-scatter-plots/, I found that, when specifying color scale with colorscale = c('hexcode1', 'hexcode2') will not have changes to the color scale, yet the default palette names such as 'Rainbow' will work. In this case, it would not be possible to self-define the 2 end points of the color scale. Please feel free to run the R codes below to see what I mean.

library(dplyr)
library(plotly)

fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec,
               marker = list(color = ~mpg, 
                             #colorscale = c('#FF0000', '#00FF72'), 
                             #colorscale ='Rainbow',
                             colorscale = c('#132B43', '#56B1F7'),
                             showscale = TRUE))
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
                                   yaxis = list(title = 'Gross horsepower'),
                                   zaxis = list(title = '1/4 mile time')),
                      annotations = list(
                        x = 1.13,
                        y = 1.05,
                        text = 'Miles/(US) gallon',
                        xref = 'paper',
                        yref = 'paper',
                        showarrow = FALSE
                      ))
fig

Just wondering if this is a bug or may be there needs some updates to the guide?

Thanks,

Activity

  1. yogat3ch commented on May 26, 2023

    @yogat3ch

    Hi @cpsievert,
    I'm wondering if there's been any progress on ensuring the colorscale argument to plot_ly and/or add_markers/trace is passed through to the plotly.js library?

    Another reprex:

    plotly::plot_ly(mtcars, x = ~ wt, y = ~ hp) |>
              plotly::add_markers(color = ~ mpg,
                                  marker = list(colorscale = c("black", "red")))

    image

    Formatted differently, it appears to apply the color gradient to the markers, but the colorbar doesn't update:

     plotly::plot_ly(mtcars, x = ~ wt, y = ~ hp) |>
              plotly::add_markers(color = ~ mpg,
                                  marker = list(colorscale = list(c(0,1), c("black", "red"))))

    image

    Using an array:

    plotly::plot_ly(mtcars, x = ~ wt, y = ~ hp) |>
              plotly::add_markers(color = ~ mpg,
                                  marker = list(colorscale = list(
                                    c(0, 0.2, 0.4, 0.6, 0.8, 1),
                                    c(
                                      "#FFB7B2",
                                      "#EA9C95",
                                      "#D68279",
                                      "#C2675C",
                                      "#AE4D40",
                                      "#9A3324"
                                    )
                                  )))

    image

    image

    1. It looks like the color spectrum is reversed, with the 1 (100th) percentile, corresponding to the lightest color rather than the darkest color.
    2. The colorbar does not update to reflect the values of the colorscale

    Are these issues in the plotly R library or in plotly js?

  2. JElchison commented on Dec 26, 2024

    @JElchison

    I think the issue may be that marker is the incorrect container for the color specs? Also, colors is not the same as colorscale.

    Try this:

    library(dplyr)
    library(plotly)
    
    fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec,
                   color = ~ mpg,
                   colors = c('#132B43', '#56B1F7'))
    fig <- fig %>% add_markers()
    fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
                                       yaxis = list(title = 'Gross horsepower'),
                                       zaxis = list(title = '1/4 mile time')),
                          annotations = list(
                            x = 1.13,
                            y = 1.05,
                            text = 'Miles/(US) gallon',
                            xref = 'paper',
                            yref = 'paper',
                            showarrow = FALSE
                          ))
    fig
    

    IMHO the real issue is the incorrect example at https://plotly.com/r/3d-scatter-plots/#3d-scatter-plot-with-color-scaling

    It should be:

    library(plotly)
    
    fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec,
                   color = ~mpg,
                   colors = c('#FFE1A1', '#683531'))
    fig <- fig %>% add_markers()
    fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
                                       yaxis = list(title = 'Gross horsepower'),
                                       zaxis = list(title = '1/4 mile time')),
                          annotations = list(
                            x = 1.13,
                            y = 1.05,
                            text = 'Miles/(US) gallon',
                            xref = 'paper',
                            yref = 'paper',
                            showarrow = FALSE
                          ))
    fig
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions