Search code examples
rplotlypca

R Plot_ly : How to change colorbar color palette and colorthreshold values?


I am trying to plot a 3D scatterplot (a PCA scatterplot of the first 3 dimensions) but I cannot find a way to change the colorbar on plot_ly.

The code is below and here is what I obtain but I would like it to go from red (most negative values) to green (upper values) with a turning point (fade pink) for a value of 0 and not +18 ‰ like it seems to be on the graph.

Can you help me?

enter image description here

ind5<-get_pca_ind(PCA_data5)
a<-ind5$coord[,1]
b<-ind5$coord[,2]
c<-ind5$coord[,3]
d<-Interviews$`Annual_SOC_rate_‰`

f1 <- list(
  family = "Times New Roman",
  size = 20,
  color = "black")


fig <- plot_ly(Interviews, x = ~a, y = ~b, z = ~c,
               marker = list(color = ~d,
                             colorbar=list(tickfont=f1),
                             showscale = TRUE))
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Dim1 (18.8%)',
                                                titlefont = f1,
                                                tickfont = f1),
                                   yaxis = list(title = 'Dim2 (16.3%)',
                                                titlefont = f1,
                                                tickfont = f1),
                                   zaxis = list(title = 'Dim3 (13.2%)',
                                                titlefont = f1,
                                                tickfont = f1)),
                      annotations = list(
                        x = 1.13,
                        y = 1.05,
                        text = 'Annual SOC change rate (‰)',
                        font=f1,
                        xref = 'paper',
                        yref = 'paper',
                        showarrow = FALSE
                      ))
fig

Solution

  • try this instead

    fig <- plot_ly(Interviews, x = ~a, y = ~b, z = ~c,
                   marker = list(color = ~d,
                                 colorbar=list(tickfont=f1),
                                 colorscale = list(c(0,'red'),
                                                   c(0.5,'pink'),
                                                   c(1, 'green'))))
    
    

    or this

    fig <- plot_ly(Interviews, x = ~a, y = ~b, z = ~c,
                   intensity = ~d,
                   colorscale = list(c(0,'red'),
                                     c(0.5,'pink'),
                                     c(1, 'green')))