user2593562
user2593562

Reputation: 116

Issue with connecting points for 3d line plot in plotly

I have a dataframe of the following format:

       x    y    z aa_num frame_num cluster
1   1.86 3.11 8.62      1         1       1
2   1.77 3.32 8.31      2         1       1
3   1.59 3.17 8.00      3         1       1
4   1.67 3.49 7.81      4         1       1
5   2.04 3.59 7.81      5         1       1
6   2.20 3.34 7.57      6         1       1
7   2.09 3.19 7.25      7         1       1
8   2.13 3.30 6.89      8         1       1
9   2.17 3.63 6.70      9         1       1
10  2.22 3.63 6.33     10         1       1
11  2.06 3.83 6.04     11         1       1
12  2.31 3.75 5.76     12         1       1
13  2.15 3.45 5.59     13         1       1
14  2.21 3.28 5.26     14         1       1
15  2.00 3.13 4.98     15         1       1
16  2.13 2.86 4.74     16         1       1
17  1.97 2.78 4.41     17         1       1
18  2.20 2.76 4.10     18         1       1
19  2.43 2.46 4.14     19         1       1
20  2.34 2.23 3.85     20         1       1
21  2.61 2.16 3.59     21         1       1
22  2.42 1.92 3.36     22         1       1
23  2.44 1.95 2.98     23         1       1
24  2.26 1.62 2.94     24         1       1
25  2.19 1.35 3.20     25         1       1
26  1.92 1.11 3.08     26         1       1
27  1.93 0.83 3.33     27         1       1
28  1.83 0.72 3.68     28         1       1
29  1.95 0.47 3.95     29         1       1
30  1.84 0.36 4.29     30         1       1
31  0.56 3.93 7.07      1         2       1
32  0.66 3.84 7.42      2         2       1
33  0.87 3.54 7.49      3         2       1
34  0.84 3.19 7.33      4         2       1
35  0.76 3.32 6.98      5         2       1
36  0.88 3.23 6.63      6         2       1
37  1.10 3.46 6.43      7         2       1
38  1.35 3.49 6.15      8         2       1
39  1.72 3.50 6.23      9         2       1
40  1.88 3.67 5.93     10         2       1
41  2.25 3.72 5.97     11         2       1
42  2.43 3.48 5.74     12         2       1
43  2.23 3.35 5.44     13         2       1
44  2.23 3.38 5.06     14         2       1
45  2.01 3.38 4.76     15         2       1
46  2.02 3.44 4.38     16         2       1
47  1.98 3.10 4.20     17         2       1
48  2.05 3.13 3.83     18         2       1
49  2.28 2.85 3.72     19         2       1
50  2.09 2.56 3.58     20         2       1
51  2.21 2.37 3.27     21         2       1
52  2.06 2.04 3.15     22         2       1
53  1.93 2.01 2.80     23         2       1
54  1.86 1.64 2.83     24         2       1
55  1.95 1.38 3.10     25         2       1
56  1.78 1.04 3.04     26         2       1
57  1.90 0.84 3.34     27         2       1
58  1.83 0.74 3.70     28         2       1
59  1.95 0.48 3.95     29         2       1
60  1.84 0.36 4.29     30         2       1
etc..

I'm trying to create a 3d line plot of this data, where a line consisting of 30 <x,y,z> points will be plotted for each frame_num and the points would be connected in the order of aa_num. The code to do this is as follows:

    fig = plot_ly(output_cl, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'lines+markers',
                   opacity = 1, line = list(width = 1, color = ~frame_num, colorscale = 'Viridis'),
                  marker = list(size = 2, color = ~frame_num, colorscale = 'Viridis'))

When I plot a single frame, it works fine:

enter image description here

However, a strange issue arises when I try to plot multiple instances.

enter image description here

For some reason, when I try to plot frame 1 and 2, point 1 and 30 connect to each other for frame 2. However, this doesn't happen for frame 1. Any ideas why? Is there someway to specify the ordering of points in 3d in plotly?

Upvotes: 0

Views: 1438

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33472

If you want to create two different traces based on column frame_num you need to pass it as a categorial variable by using factor. As an alternative you can use name = ~ frame_num or split = ~ frame_num to create multiple traces.

library(plotly)

output_cl <- data.frame(
           x = c(1.86,1.77,1.59,1.67,2.04,2.2,2.09,
                 2.13,2.17,2.22,2.06,2.31,2.15,2.21,2,2.13,1.97,2.2,
                 2.43,2.34,2.61,2.42,2.44,2.26,2.19,1.92,1.93,1.83,1.95,
                 1.84,0.56,0.66,0.87,0.84,0.76,0.88,1.1,1.35,1.72,1.88,
                 2.25,2.43,2.23,2.23,2.01,2.02,1.98,2.05,2.28,2.09,
                 2.21,2.06,1.93,1.86,1.95,1.78,1.9,1.83,1.95,1.84),
           y = c(3.11,3.32,3.17,3.49,3.59,3.34,3.19,
                 3.3,3.63,3.63,3.83,3.75,3.45,3.28,3.13,2.86,2.78,2.76,
                 2.46,2.23,2.16,1.92,1.95,1.62,1.35,1.11,0.83,0.72,
                 0.47,0.36,3.93,3.84,3.54,3.19,3.32,3.23,3.46,3.49,3.5,
                 3.67,3.72,3.48,3.35,3.38,3.38,3.44,3.1,3.13,2.85,2.56,
                 2.37,2.04,2.01,1.64,1.38,1.04,0.84,0.74,0.48,0.36),
           z = c(8.62,8.31,8,7.81,7.81,7.57,7.25,6.89,
                 6.7,6.33,6.04,5.76,5.59,5.26,4.98,4.74,4.41,4.1,
                 4.14,3.85,3.59,3.36,2.98,2.94,3.2,3.08,3.33,3.68,3.95,
                 4.29,7.07,7.42,7.49,7.33,6.98,6.63,6.43,6.15,6.23,5.93,
                 5.97,5.74,5.44,5.06,4.76,4.38,4.2,3.83,3.72,3.58,
                 3.27,3.15,2.8,2.83,3.1,3.04,3.34,3.7,3.95,4.29),
      aa_num = c(1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,
                 11L,12L,13L,14L,15L,16L,17L,18L,19L,20L,21L,22L,23L,
                 24L,25L,26L,27L,28L,29L,30L,1L,2L,3L,4L,5L,6L,7L,
                 8L,9L,10L,11L,12L,13L,14L,15L,16L,17L,18L,19L,20L,
                 21L,22L,23L,24L,25L,26L,27L,28L,29L,30L),
   frame_num = c(1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                 1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,
                 2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,
                 2L,2L),
     cluster = c(1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                 1L,1L)
)

fig = plot_ly(output_cl, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'lines+markers', color = ~factor(frame_num), # or name = ~ frame_num or split = ~ frame_num
              colors ="Set2", opacity = 1, line = list(width = 1), marker = list(size = 2))
fig

result

Upvotes: 1

Related Questions