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.
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