2

I am trying to add a text annotation to points in a scatter3d Plotly plot with a different scene. How can I make the annotation move around with the plot? Even when I sign xref and yref as 'scene' the annotation doesn't move.

This is a reproducible example I am trying to run:

library(plotly)

rep.ex = data.frame(x1=1:20, y1=1:20, z1=(1:20)*2, text1=letters[1:20])

axoff <- list(title = "", zeroline = FALSE, showline = FALSE, showticklabels = FALSE, showgrid = FALSE, autotick = F)
axoffxy <- list(title = "", zeroline = FALSE, showline = FALSE, showticklabels = FALSE, showgrid = FALSE, autotick = F, showspikes=F)


plot_ly(data=data.frame(rep.ex), x=rep.ex$x1, y=rep.ex$y1, z=rep.ex$z1, 
            marker=list(size=2.6), 
            color=rep.ex$x1, hoverinfo='text',
            text=rep.ex$text1,
            type="scatter3d", mode = "markers") %>%
  layout(showlegend = T, dragmode="turntable", scene = list(aspectmode='cube', xaxis = axoffxy, yaxis = axoffxy, zaxis = axoff), 
         annotations=list(showarrow=FALSE, 
                          text='Here I insert my annotation',  
                          xref='scene',     
                          yref='scene',
                          zref='scene',
                          x=1,  
                          y=1, 
                          z=2,
                          xanchor='left',   
                          yanchor='bottom',  
                          font=list(size=12 )))

I am using Plotly version 4.9.0

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
KGee
  • 771
  • 7
  • 26

1 Answers1

2

If your problem allows you to switch from annotations in layout to add_trace it will move around. Something like this will work for that:

plot_ly(data=data.frame(rep.ex), x=rep.ex$x1, y=rep.ex$y1, z=rep.ex$z1, 
        marker=list(size=2.6), color=rep.ex$x1, hoverinfo='text',
        text=rep.ex$text1, type="scatter3d", mode = "markers", showlegend=F) %>%
  add_trace(type='scatter3d', mode='text', x=10,y=10,z=10, 
            text='Here I insert my annotation', showlegend=F, inherit=F) %>%
  layout(showlegend = T, dragmode="turntable", 
         scene = list(aspectmode='cube', xaxis = axoffxy, yaxis = axoffxy, zaxis = axoff))
bobbel
  • 1,983
  • 6
  • 21
  • I suppose this will do for now, I would really have liked arrows which did seem to work in an earlier version of plotly but not anymore. (I'll mark as correct unless somebody else comes up with an annotations version in the next few days) – KGee Nov 06 '19 at 12:36
  • The same approach was used [here](https://stackoverflow.com/questions/37596865/add-annotation-to-3d-scatterplot-in-plotly) – ismirsehregal Nov 07 '19 at 13:04