简体   繁体   中英

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

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

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM