0

I have three coordinates variables and one continuous variable. I would like to get a 3d scatter plot condition on specific values of the continuous variable.

Example of my data is:

A = c( 8.3, 7.5, 8.0, 7.1  6.5, 7.4)

x = c(147.2818, 147.2818, 147.2779, 147.2779, 147.2337, 147.1693)

y = c( 147.2818, 147.2818, 147.2779, 147.2779, 147.2337, 147.1693)

z = c( 22, 21, 22, 22, 30, 26) 

I would like to get the 3d a scatter plot conditioned on the values of A.

For example, if the values of A is between 8 to 8.5 then the colour is red.

if the values of A is between 7:7.5 then the colour is blue.

finally, if the values of A is between 6:6.5 then the colour is green.

My data contain about 3000 observation. So, I just provide an example of my problem. Any help, please?

Please note that I used the ploty function but as my data is very large, the output was not clear and not helpful.

Mary
  • 78
  • 1
  • 11
  • See [this](https://stackoverflow.com/questions/45052188/how-to-plot-3d-scatter-diagram-using-ggplot) one. – maydin Aug 15 '19 at 12:02
  • I used it but as my data is very large, so this makes the plot unhelpful and unclear. So, the best way is to consider the level of the values (based on a specific range of values to make it clear to the reader). – Mary Aug 15 '19 at 12:08

2 Answers2

1

You can do it with ifelse function,

A = c( 8.3, 7.5, 8.0 ,7.1 , 6.5, 7.4)

x = c(147.2818, 147.2818, 147.2779, 147.2779, 147.2337, 147.1693)

y = c( 147.2818, 147.2818, 147.2779, 147.2779, 147.2337, 147.1693)

z = c( 22, 21, 22, 22, 30, 26)

my_color <- ifelse(A<=8.5 & A>8,"red", ifelse(A<=7.5 & A>7,"blue","green"))

plot_ly(x=x, y=y, z=z, type="scatter3d", mode="markers", color=my_color)

enter image description here

Additionally, if you don't want to assign it by using ifelse, you can make clusters and assign it to the plotly like,

n =3 # number of clusters

my_col_cluster <- kmeans(A,n)$cluster

plot_ly(x=x, y=y, z=z, type="scatter3d", mode="markers", color= my_col_cluster)

enter image description here

maydin
  • 3,715
  • 3
  • 10
  • 27
  • This help, however, how about 6.5. For example, if I would like to add another ifelse. – Mary Aug 15 '19 at 12:30
  • @Mary 6.5 assigned as green since it is out of the conditions. The mistake in here, 8 also assigned as green since it is not included inside the conditions. If you define the boundaries of the conditions specifically with ending of one is starting of the other, then every number will be assigned correctly. – maydin Aug 15 '19 at 12:36
  • Do you mean like this: `ifelse(A > 8,"red", ifelse(A<=7.5 "blue", ifelse (A <= 6.5))? if not, could you please help me with this point? – Mary Aug 15 '19 at 12:41
  • @Mary For example , `my_color <- ifelse(A<=8.5 & A>7.5,"red", ifelse(A<=7.5 & A>7,"blue","green"))` . The ends of one `A>7.5` is the starting of the next `A<=7.5`. But as I suggested, if you don't like it you can just use clusters as well. – maydin Aug 15 '19 at 12:44
0

You could try the plot3D library

install.packages("plot3D")
library(plot3D)

## Use ifelse() to create color labels
colVar <- sapply(A,function(a){ifelse(a>=6&a<=6.5,'green',ifelse(a>=7&a<=7.5,'blue','red'))})
colVar <- factor(colVar,levels=c('green','blue','red'))

## Plot using plot3D(). 
## colvar takes integer values for color groups.
## colkey creates the legend
## col sets the color scheme to each group index
scatter3D(x=x,y=y,z=z,
      colvar=as.integer(colVar),
      colkey=list(at=c(1,2,3),side=4),
      col=as.character(levels(colVar)),
      pch=19)

enter image description here

P1storius
  • 917
  • 5
  • 12