# Add Lines to plot\_ly 3D NMDS Ordination

**URL:** <https://community.plotly.com/t/add-lines-to-plot-ly-3d-nmds-ordination/75366>\
**Category:** Plotly R\
**Tags:** question\
**Created:** [May 17, 2023, 8:47pm UTC](https://community.plotly.com/t/add-lines-to-plot-ly-3d-nmds-ordination/75366 "2023-05-17T20:47:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdrianEcology](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@AdrianEcology](https://community.plotly.com/u/AdrianEcology)\
**Post date:** [May 17, 2023, 8:47pm UTC](https://community.plotly.com/t/add-lines-to-plot-ly-3d-nmds-ordination/75366/1 "2023-05-17T20:47:45Z")

</div>

Hi, I would like to add lines of 5 categories that are driving seasonal shifts in the dietary composition of my study species, using the plot\_ly package in R.

I successfully made the 3D ordination but when I tried using the add\_trace() function, the lines appeared with the appropriate category names, but the color palette for both the points as well as the lines, is incorrect. Any help would be greatly appreciated!

> # Add arrows to the plot to indicate which environmental variables are responsible for the shifts in the pattern
> 
> plot\_arrow ← data.frame(NMDS1 = rep(0,10), NMDS2 = rep(0,10), NMDS3 = rep(0,10))  
> plot\_arrow[6:10,] ← as.data.frame(scores(envfit.3D, “vectors”)) \* exp(0.5)  
> plot\_arrow$food.items ← c(“Grasses”, “Legumes”, “Herbs”, “Fruit”, “Animals”,“Grasses”, “Legumes”, “Herbs”, “Fruit”, “Animals”)
> 
> pal ← c(“skyblue”, “darkgreen”, “brown”, “orange”)  
> scores.3D$season ← factor(scores.3D$season, levels = c(“Winter”, “Spring”, “Summer”, “Fall”))  
> plot.3d ← plot\_ly(scores.3D, x=~NMDS1, y=~NMDS2, z=~NMDS3,  
> type=“scatter3d”, mode=“markers”,  
> color = as.factor(scores.3D$season),  
> colors = pal)  
> plot.3d

 ![plot.3d](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/7/7/77d15a525edbf746e41562f524371e2d437470cd.jpeg)

> food.pal ← c(“forestgreen”, “blue”, “grey25”, “purple”, “red”)  
> plot\_arrow$food.items ← factor(plot\_arrow$food.items, levels = c(“Grasses”, “Legumes”, “Herbs”, “Fruit”, “Animals”))  
> plot.3d.arrows ← add\_trace(plot.3d, x = ~plot\_arrow$NMDS1, y = ~plot\_arrow$NMDS2, z = ~plot\_arrow$NMDS3,  
> type=“scatter3d”, mode=“lines”,  
> color = as.factor(plot\_arrow$food.items),  
> colors = food.pal)  
> plot.3d.arrows
> 
> ![plot.3d.arrows](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/f/e/fee6e54cf34451ed40aad7c27c7179be8803480a.jpeg)

What confuses me is that when I plot the line segments on their own using the below code, the lines come out fine and with the appropriate color palette.

> plot.3d.arrows2 ← plot\_ly(scores.3D, x = ~plot\_arrow$NMDS1, y = ~plot\_arrow$NMDS2, z = ~plot\_arrow$NMDS3,  
> type=“scatter3d”, mode=“lines”,  
> color = as.factor(plot\_arrow$food.items),  
> colors = food.pal)  
> plot.3d.arrows2
> 
> ![plot.3d.arrows2](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/f/5/f56a61847fcfb9c7298b70474028242bb9fe3264.jpeg)

Again, any help would be greatly appreciated!
