# Colors not displaying correctly on a 3D scatterplot (R)

**URL:** <https://community.plotly.com/t/colors-not-displaying-correctly-on-a-3d-scatterplot-r/36047>\
**Category:** Plotly R\
**Created:** [March 10, 2020, 12:02pm UTC](https://community.plotly.com/t/colors-not-displaying-correctly-on-a-3d-scatterplot-r/36047 "2020-03-10T12:02:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdplum](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@bdplum](https://community.plotly.com/u/bdplum)\
**Post date:** [March 10, 2020, 12:02pm UTC](https://community.plotly.com/t/colors-not-displaying-correctly-on-a-3d-scatterplot-r/36047/1 "2020-03-10T12:02:44Z")

</div>

I am trying to make a couple of plots (code below) and to keep the colors consistent between the two graphs. One graph contains 8 data points and the other contains 5. Despite using almost identical code for each of the plots, the colors do not match up. Does anyone have any insight as to why the colors are not matching up between the two graphs?

Correct colors ([https://plot.ly/~MKT533/3/#/](https://plot.ly/~MKT533/3/#/)):

p \<- plot\_ly(mkt533, x = ~onsitetrainers\_x, y = ~lowprice\_y, z = ~flexibleclasses\_z, color = ~name, colors = c(’#AB1100’,’#00274C’,’#00B5AF’,’#00274C’,’#00274C’,’#72088E’,’#E9B000’,’#0050AC’)) %\>%  
add\_markers() %\>%  
layout(scene = list(xaxis = list(title = ‘Professional guidance’, range=c(1,10)),  
yaxis = list(title = ‘Value for money’, range=c(1,10)),  
zaxis = list(title = ‘Time flexibility’, range=c(1,10))))

Wrong colors ([https://plot.ly/~MKT533/1/#/](https://plot.ly/~MKT533/1/#/)):

mkt533\_product \<- subset(mkt533, type!=“Segment”)

product \<- plot\_ly(mkt533\_product, x = ~onsitetrainers\_x, y = ~lowprice\_y, z = ~flexibleclasses\_z, color = ~name, colors = c(’#AB1100’,’#00B5AF’,’#72088E’,’#E9B000’,’#0050AC’)) %\>%  
add\_markers() %\>%  
layout(scene = list(xaxis = list(title = ‘Professional guidance’, range=c(1,10)),  
yaxis = list(title = ‘Value for money’, range=c(1,10)),  
zaxis = list(title = ‘Time flexibility’, range=c(1,10))))

---

<div class="post-metadata">

**Author:** ![ismirsehregal](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/ismirsehregal/32/6794_2.png) [@ismirsehregal](https://community.plotly.com/u/ismirsehregal)\
**Post date:** [March 10, 2020, 12:24pm UTC](https://community.plotly.com/t/colors-not-displaying-correctly-on-a-3d-scatterplot-r/36047/2 "2020-03-10T12:24:55Z")

</div>

Can you please share the relevant data.frames by e.g. using `dput(mkt533)`.

---

<div class="post-metadata">

**Author:** ![bdplum](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@bdplum](https://community.plotly.com/u/bdplum)\
**Post date:** [March 10, 2020, 12:40pm UTC](https://community.plotly.com/t/colors-not-displaying-correctly-on-a-3d-scatterplot-r/36047/3 "2020-03-10T12:40:54Z")

</div>

Sorry about that. Here is mkt533

structure(list(onsitetrainers\_x = c(1L, 3L, 10L, 9L, 2L, 1L,  
7L, 10L), lowprice\_y = c(10L, 3L, 3L, 2L, 7L, 7L, 3L, 1L), flexibleclasses\_z = c(4L,  
8L, 3L, 5L, 7L, 1L, 6L, 6L), name = structure(c(4L, 2L, 5L, 3L,  
7L, 1L, 8L, 6L), .Label = c(“At-home gym”, “Busy young families”,  
“CrossFit”, “Fitness-conscious youth”, “Need that extra push”,  
“Taekwondo gym”, “YMCA”, “Yoga studio”), class = “factor”), type = structure(c(3L,  
3L, 3L, 2L, 5L, 1L, 6L, 4L), .Label = c(“At-home gym”, “CrossFit”,  
“Segment”, “Taekwondo gym”, “YMCA”, “Yoga studio”), class = “factor”),  
size = c(0.55, 0.3, 0.15, 0.25, 0.25, 0.25, 0.25, 0.25)), class = “data.frame”, row.names = c(NA,  
-8L))

And mkt533\_product  
structure(list(onsitetrainers\_x = c(9L, 2L, 1L, 7L, 10L), lowprice\_y = c(2L,  
7L, 7L, 3L, 1L), flexibleclasses\_z = c(5L, 7L, 1L, 6L, 6L), name = structure(c(3L,  
7L, 1L, 8L, 6L), .Label = c(“At-home gym”, “Busy young families”,  
“CrossFit”, “Fitness-conscious youth”, “Need that extra push”,  
“Taekwondo gym”, “YMCA”, “Yoga studio”), class = “factor”), type = structure(c(2L,  
5L, 1L, 6L, 4L), .Label = c(“At-home gym”, “CrossFit”, “Segment”,  
“Taekwondo gym”, “YMCA”, “Yoga studio”), class = “factor”), size = c(0.25,  
0.25, 0.25, 0.25, 0.25)), row.names = 4:8, class = “data.frame”)
