colorscale does not change the scatter points color in plotly 3d scatterplot in R

Viewed 107

I am learning to plot in r using plotly package. This chunk of code is provided on plotly website: enter link description here . I change colorscale = c('#FFE1A1', '#683531') to let say colorscale = c('blue', 'red'). shouldnt it change the scatter points colors? what part of the code is then supposed to change the color of the points gradiently. I know I can set it to pallette names or like "Greens", but I dont know how to give a range of gradient colors between two.


also as far as I looked, plotly 3d points can not have a sphere effecct. just wondering if I can mix it with rgl package who has.

library(plotly)

fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec,
               marker = list(color = ~mpg, colorscale = c('#FFE1A1', '#683531'), 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
1 Answers

I had the exact same issue today, you can create your scale into a list before using colorscale:

colorscale <- list(c(0, 1), c("blue", "red"))
# or
colorscale <- list(c(0, 1), c("#FFE1A1", "#683531"))

Full code

fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec,
               marker = list(color = ~mpg, colorscale=list(c(0, 1), c("blue", "red")), 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

enter image description here

Related