title: Add legend to plot3d in R
body: <p>I have visualized cluster result as shown below: </p>
  
  <pre><code>plot3d(RFM[,2:4],col= RFM$CLASS)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ZBQ1K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZBQ1K.png" alt="enter image description here"></a></p>
  
  <p>now how can I add legend for cluster labels (CLASS) with corresponding colors?</p>
  
  <p>My data is like following :</p>
  
  <p><a href="https://i.stack.imgur.com/VrAev.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VrAev.png" alt="enter image description here"></a></p>
  
  <p>edit: </p>
  
  <p>I have tried legend3d and text3d and I can add legend, but the problem is that I can not be sure the color of legend is the same as the color of the shape as they are corresponding to a given class !, how can I do it in correct way ?</p>
  
  <p>Solution for me: 
  I got a solution that worked for me, as I said I wanted to get sure colors remain same on both legend and shape, so I did this:</p>
  
  <pre><code>   plot3d(RFM[RFM$CLASS==1,2:4],col = "red" )
  plot3d(RFM[RFM$CLASS==2,2:4],col = "blue", add=T )
  plot3d(RFM[RFM$CLASS==3,2:4],col = "green", add=T )
  plot3d(RFM[RFM$CLASS==4,2:4],col = "cyan", add=T )
  plot3d(RFM[RFM$CLASS==5,2:4],col = "yellow", add=T )
  
  legend3d("topright", legend = paste('Type', c('1','2','3','4','5')), pch = 16, col = c("red","blue","green","cyan","yellow")  , cex=1, inset=c(0.02))
  </code></pre>