Sankey plot with plotly in R: how do I get the plot to skip over the NAs and not try to plot dead ends of some nodes?

huangapple go评论62阅读模式
英文:

Sankey plot with plotly in R: how do I get the plot to skip over the NAs and not try to plot dead ends of some nodes?

问题

我已翻译代码和数据的主要部分,如下所示:

#输出结构细节
str(sank_test)

'data.frame':	168 obs. of  12 variables:
 $ GobySpecies: int  1 1 1 1 1 1 1 2 2 2 ...
 $ X2014      : chr  "An4" "Ate4" "Ate4" "Ate4" ...
 $ X2015      : chr  "Ate5" "Ate5" "Ate5" "Ate5" ...
 $ X2016      : chr  "An6" "Ate6" "Ate6" "Ate6" ...
 $ X2018      : chr  "An8" "Ate8" "Av8" "Ac8" ...
 $ X2020      : chr  "" "" "" "" ...
 $ n          : int  10 29 12 29 12 1 7 25 18 4 ...
 $ color      : chr  "red" "red" "red" "red" ...
 $ color.1    : chr  "red" "red" "red" "red" ...
 $ color.2    : chr  "red" "red" "red" "red" ...
 $ color.3    : chr  "red" "red" "red" "red" ...
 $ color.4    : chr  "red" "red" "red" "red" ...

list.of.packages <- c("ggplot2","plotly","dplyr")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
#load specific packages of interest
lapply(list.of.packages,library,character.only = TRUE)

nodes_test <- data.frame(name = unique(c(as.character(sank_test$GobySpecies),
                                    as.character(sank_test$X2014),
                                    as.character(sank_test$X2015),
                                    as.character(sank_test$X2016),
                                    as.character(sank_test$X2018),
                                    as.character(sank_test$X2020))))

# create links_test dataframe
links_test <- data.frame(source = match(sank_test$GobySpecies, nodes_test$name) - 1,
                    target = match(sank_test$X2014, nodes_test$name) - 1,
                    value = sank_test$n,
                    stringsAsFactors = FALSE)

links_test <- rbind(links_test,
               data.frame(source = match(sank_test$X2014, nodes_test$name) - 1,
                          target = match(sank_test$X2015, nodes_test$name) - 1,
                          value = sank_test$n,
                          stringsAsFactors = FALSE))
links_test <- rbind(links_test,
               data.frame(source = match(sank_test$X2015, nodes_test$name) - 1,
                          target = match(sank_test$X2016, nodes_test$name) - 1,
                          value = sank_test$n,
                          stringsAsFactors = FALSE))

links_test <- rbind(links_test,
               data.frame(source = match(sank_test$X2016, nodes_test$name) - 1,
                          target = match(sank_test$X2018, nodes_test$name) - 1,
                          value = sank_test$n,
                          stringsAsFactors = FALSE))

links_test <- rbind(links_test,
               data.frame(source = match(sank_test$X2018, nodes_test$name) - 1,
                          target = match(sank_test$X2020, nodes_test$name) - 1,
                          value = sank_test$n,
                          stringsAsFactors = FALSE))


plot_ly(
  type = "sankey",
  orientation = "h",
  node = list(pad = 15,
              thickness = 20,
              line = list(color = "black", width = 0.5),
              label = nodes_test$name,color="black"),
  link = list(source = links_test$source,
              target = links_test$target,
              value = links_test$value,color = c(sank_test$color,sank_test$color.1,sank_test$color.2,sank_test$color.3,sank_test$color.4)),
  textfont = list(size = 10),
  width = 1000,
  height = 480
) 

# Code
#make node colors same as link colors
plot_ly(
  type = "sankey",
  orientation = "...

请注意,由于限制,我无法提供完整的代码和输出。如果您需要更多帮助或有其他问题,请告诉我。

英文:

I created a sankey plot with Plotly in R. I am happy with the color match up between the links and nodes (purposely only have the first column of nodes to be color matched up, and rest of the columns to be in black). However, some nodes are supposed to have dead ends for some links, which for some reason is not plotting correctly. Instead, my code is making a full loop and added a new node there (or shifted it over) for that loop (see bottom right corner with the purple, blue and yellow etc. looping). How do I get rid of that? You'll notice my dataframe has blanks in the columns where it should dead end. Later down in this question, I show that I figured out how to get rid of that looping node, but then I lost the follow up data (species 2 in orange), see below. Thanks in advance.

Sankey plot with plotly in R: how do I get the plot to skip over the NAs and not try to plot dead ends of some nodes?

Here is the head and tail of my data and then my code below. Each color corresponds to a specific GobySpecies for links and matches up with the node coloring that I added in my code in the plot_ly function:


head(sank_test,100)
GobySpecies X2014 X2015 X2016 X2018  X2020  n  color color.1 color.2 color.3
1            1   An4  Ate5   An6   An8        10    red     red     red     red
2            1  Ate4  Ate5  Ate6  Ate8        29    red     red     red     red
3            1  Ate4  Ate5  Ate6   Av8        12    red     red     red     red
4            1  Ate4  Ate5  Ate6   Ac8        29    red     red     red     red
5            1  Ate4  Ate5  Ate6   Al8        12    red     red     red     red
6            1  Ate4  Ate5   An6   An8         1    red     red     red     red
7            1  Ate4  Ate5  Ate6   An8         7    red     red     red     red
8            2   An4   An5   An6         An20 25 orange  orange  orange  orange
9            2   Am4   Am5   Am6         Ac20 18 orange  orange  orange  orange
10           2   Am4   An5   Am6       Aspp20  4 orange  orange  orange  orange
11           2   An4   Ac5   Ac6         Ac20  6 orange  orange  orange  orange
12           2   An4   An5   Ac6         Ac20  5 orange  orange  orange  orange
13           2   An4   An5   Ag6        Ase20  2 orange  orange  orange  orange
14           2   An4   An5   Ag6         Ac20 20 orange  orange  orange  orange
15           2   An4   An5   Al6         Al20  2 orange  orange  orange  orange
16           2   An4   An5   Al6         Ac20  4 orange  orange  orange  orange
17           2   An4   Av5   Av6         Ac20  6 orange  orange  orange  orange
18           2   An4   An5   An6         Ac20  8 orange  orange  orange  orange
19           3   Am4   Am5   Am6               6 yellow  yellow  yellow  yellow
20           3  Ato4  Ato5  Ato6               5 yellow  yellow  yellow  yellow
21           3 Aspp4 Aspp5 Aspp6              12 yellow  yellow  yellow  yellow
22           3   Am4   Am5   Ag6               7 yellow  yellow  yellow  yellow
23           3  Ato4   Am5  Ato6               3 yellow  yellow  yellow  yellow
24           3 Aspp4 Aspp5  Ato6               1 yellow  yellow  yellow  yellow
25           3  Ato4 Aspp5  Ato6               8 yellow  yellow  yellow  yellow
26           3  Ato4   An5   An6              29 yellow  yellow  yellow  yellow
27           3  Ato4   Ag5   Ag6              13 yellow  yellow  yellow  yellow
28           3  Ato4   Ar5   Ag6               3 yellow  yellow  yellow  yellow
29           3  Ato4  Ate5   Ag6               5 yellow  yellow  yellow  yellow
30           3  Ato4   Av5   Ag6               1 yellow  yellow  yellow  yellow
31           3  Ato4   Av5  Ato6               2 yellow  yellow  yellow  yellow
32           3  Ato4   Ac5  Ato6               5 yellow  yellow  yellow  yellow
33           4   Ac4   Ac5   Ac6   Ac8   Ac20  1  green   green   green   green
34           4   Al4   Al5   Al6   Al8   Al20  7  green   green   green   green
35           4   Ar4   Ar5   Ar6   Ar8   Ar20  2  green   green   green   green
36           4  Ate4  Ate5  Ate6  Ate8  Ate20  3  green   green   green   green
37           4   An4   An5   An6   An8   Ac20  5  green   green   green   green
38           4   Al4   Av5   Av6   Av8   Av20  9  green   green   green   green
39           4   Al4   Al5   Al6   Ag8   Ac20 10  green   green   green   green
40           4  Ate4  Ate5  Ato6  Ate8  Ate20  1  green   green   green   green
41           4 Aspp4   Ar5   Al6  Ate8 Aspp20  3  green   green   green   green
42           4   Ar4   Ar5   Av6   Ar8   Av20  1  green   green   green   green
43           4   Ac4   Al5   Al6   Ac8   Ac20  2  green   green   green   green
44           4   An4   An5   Al6   An8   Ac20  4  green   green   green   green
45           4  Ate4  Ate5   Al6  Ate8  Ate20  5  green   green   green   green
46           4   Al4   Al5   Al6   Al8   Ac20  2  green   green   green   green
47           4   An4   Al5   Al6   Ar8 Aspp20  6  green   green   green   green
48           4  Ate4   Av5   Av6  Ate8  Ase20  2  green   green   green   green
49           4   Al4   Al5   Al6   Ac8   Ac20  6  green   green   green   green
50           4   An4   Al5   Al6   Ac8   Ac20  2  green   green   green   green
51           4  Ate4   Ar5   Al6  Ate8   Av20 13  green   green   green   green
52           4  Ate4   Av5   Al6   Ac8   Av20  5  green   green   green   green
53           4   An4   Al5   Al6   Ac8 Aspp20  6  green   green   green   green
54           4  Ate4   Av5   Al6   Ac8 Aspp20  3  green   green   green   green
55           4  Ate4   Al5   Al6   Ac8 Aspp20  2  green   green   green   green
56           5   Ac4   Ac5   Ac6   Ac8   Ac20  5   cyan    cyan    cyan    cyan
57           5   An4   An5   An6   An8   An20 10   cyan    cyan    cyan    cyan
58           5  Ate4  Ate5  Ate6  Ate8  Ate20  1   cyan    cyan    cyan    cyan
59           5   Ag4   Ag5   Ag6   Ac8   Ac20  1   cyan    cyan    cyan    cyan
60           5   Ag4   Ag5   Ac6   Ac8   Ac20  2   cyan    cyan    cyan    cyan
61           5  Ate4  Ate5  Ate6  Ate8   Ac20  2   cyan    cyan    cyan    cyan
62           5  Ate4  Ate5   Al6  Ate8   Ac20  4   cyan    cyan    cyan    cyan
63           5   Al4   Al5   Al6   Ac8   Al20  1   cyan    cyan    cyan    cyan
64           5   Al4   Al5   Al6   Ac8   Ac20  1   cyan    cyan    cyan    cyan
65           5  Ate4   Am5   An6  Ate8   Ac20  4   cyan    cyan    cyan    cyan
66           5 Aspp4   Av5   Av6 Aspp8 Aspp20  1   cyan    cyan    cyan    cyan
67           5 Aspp4   Al5   Al6 Aspp8 Aspp20  2   cyan    cyan    cyan    cyan
68           5   Am4   Am5   Am6  Ate8   Ac20 32   cyan    cyan    cyan    cyan
69           5   An4   An5   An6   Ac8   An20 10   cyan    cyan    cyan    cyan
70           5   An4   An5   An6   Ac8   Ac20 11   cyan    cyan    cyan    cyan
71           5   An4   An5   Av6  Ase8  Ase20  1   cyan    cyan    cyan    cyan
72           5   An4   An5   Av6  Ase8   Ar20  1   cyan    cyan    cyan    cyan
73           5   An4   An5   Av6  Ase8   Ac20  3   cyan    cyan    cyan    cyan
74           5   An4   Ac5   Al6   Ac8   Ac20  2   cyan    cyan    cyan    cyan
75           5   An4   An5   Al6  Ate8   Ac20  1   cyan    cyan    cyan    cyan
76           5   An4   An5   Ac6  Ate8   Ac20  3   cyan    cyan    cyan    cyan
77           5   Am4   Am5   Av6 Aspp8   Ac20  1   cyan    cyan    cyan    cyan
78           5   Am4   Am5 Aspp6 Aspp8   Ac20  1   cyan    cyan    cyan    cyan
79           6   Am4   Am5   Am6              50   blue    blue    blue    blue
80           6   An4   An5   An6              14   blue    blue    blue    blue
81           6   Ag4   Ag5   Am6               4   blue    blue    blue    blue
82           6   Ac4   Am5   Am6               4   blue    blue    blue    blue
83           6 Aspp4   Am5   Am6               4   blue    blue    blue    blue
color.4
1      red
2      red
3      red
4      red
5      red
6      red
7      red
8   orange
9   orange
10  orange
11  orange
12  orange
13  orange
14  orange
15  orange
16  orange
17  orange
18  orange
19  yellow
20  yellow
21  yellow
22  yellow
23  yellow
24  yellow
25  yellow
26  yellow
27  yellow
28  yellow
29  yellow
30  yellow
31  yellow
32  yellow
33   green
34   green
35   green
36   green
37   green
38   green
39   green
40   green
41   green
42   green
43   green
44   green
45   green
46   green
47   green
48   green
49   green
50   green
51   green
52   green
53   green
54   green
55   green
56    cyan
57    cyan
58    cyan
59    cyan
60    cyan
61    cyan
62    cyan
63    cyan
64    cyan
65    cyan
66    cyan
67    cyan
68    cyan
69    cyan
70    cyan
71    cyan
72    cyan
73    cyan
74    cyan
75    cyan
76    cyan
77    cyan
78    cyan
79    blue
80    blue
81    blue
82    blue
83    blue
tail(sank_test,80)
GobySpecies X2014 X2015 X2016 X2018  X2020  n  color color.1 color.2 color.3
89            6   Ag4   Av5   Av6               2   blue    blue    blue    blue
90            6   Ag4   Al5  Ato6               2   blue    blue    blue    blue
91            7   Ac4   Ac5   Ac6   Ac8   Ac20  8 purple  purple  purple  purple
92            7   An4   An5   An6   Ac8   An20 10 purple  purple  purple  purple
93            7   An4   An5   An6   Ac8   Ac20 42 purple  purple  purple  purple
94            7   Ac4   Ar5   Ac6  Ate8  Ate20  1 purple  purple  purple  purple
95            7   Ac4  Ato5   Ac6  Ate8  Ate20  1 purple  purple  purple  purple
96            7   Al4   Al5   Al6   Av8   Al20  2 purple  purple  purple  purple
97            7   Am4   Am5   Am6   Av8   Av20  2 purple  purple  purple  purple
98            7   An4   Al5   Av6   Av8   Av20  2 purple  purple  purple  purple
99            7   An4   Al5   Al6   Av8   Ac20  8 purple  purple  purple  purple
100           7 Aspp4   An5   Am6  Ate8 Aspp20  2 purple  purple  purple  purple
101           7   An4   Am5   Am6  Ate8   Al20  1 purple  purple  purple  purple
102           7   An4   Am5   Am6  Ate8   Av20  1 purple  purple  purple  purple
103           7   An4   Am5   Am6  Ate8 Aspp20  1 purple  purple  purple  purple
104           7   An4   An5   Av6   Av8   Ac20  1 purple  purple  purple  purple
105           7   An4   An5   Ac6  Ate8   Ac20  1 purple  purple  purple  purple
106           7   An4   Ag5   Ag6  Ate8   Ac20  3 purple  purple  purple  purple
107           7   An4   An5  Ate6  Ate8   Ac20  2 purple  purple  purple  purple
108           7   An4   An5   An6   Av8   Ac20  1 purple  purple  purple  purple
109           7   An4   An5   Am6  Ate8   Ac20  3 purple  purple  purple  purple
110           7   An4   Am5   Am6   Av8   Ac20  4 purple  purple  purple  purple
111           7   An4   Am5   Am6  Ate8   Ac20  4 purple  purple  purple  purple
112           8   Al4   Al5   Al6              10 violet  violet  violet  violet
113           8   Av4   Av5   Av6              12 violet  violet  violet  violet
114           8   Ag4   Al5   Al6               7 violet  violet  violet  violet
115           8  Ate4   Ar5   Al6              10 violet  violet  violet  violet
116           8 Aspp4   Ar5   Al6              23 violet  violet  violet  violet
117           8   Av4   Ar5   Av6               2 violet  violet  violet  violet
118           8   An4   Al5   Al6              22 violet  violet  violet  violet
119           8   An4   Al5   An6               1 violet  violet  violet  violet
120           8   Av4   Ar5   Al6              12 violet  violet  violet  violet
121           8   Av4   Al5   Al6               1 violet  violet  violet  violet
122           9   Al4   Al5   Al6   Al8   Al20  1   grey    grey    grey    grey
123           9   Am4   Am5   Am6   Am8   Am20  4   grey    grey    grey    grey
124           9   An4   An5   An6   An8   An20  3   grey    grey    grey    grey
125           9   Av4   Av5   Av6   Av8   Av20  3   grey    grey    grey    grey
126           9 Aspp4 Aspp5 Aspp6 Aspp8 Aspp20  5   grey    grey    grey    grey
127           9   An4   An5   An6   An8   Ac20 13   grey    grey    grey    grey
128           9   An4   An5   An6  Ase8  Ase20 16   grey    grey    grey    grey
129           9   Am4   Am5   Am6   Am8  Ase20  2   grey    grey    grey    grey
130           9  Ate4  Ate5  Ate6  Ate8   Ac20 10   grey    grey    grey    grey
131           9   Am4   Am5 Aspp6   Am8 Aspp20  6   grey    grey    grey    grey
132           9  Ato4  Ato5  Ato6   Av8  Ase20  3   grey    grey    grey    grey
133           9   Am4  Ato5  Ato6   Am8  Ase20  3   grey    grey    grey    grey
134           9   Ag4   Ag5   Ag6  Ate8   Ag20  3   grey    grey    grey    grey
135           9   Ag4   Ag5   Ag6   Al8   Al20  2   grey    grey    grey    grey
136           9   Ag4   Al5   Ag6   Al8   Al20  1   grey    grey    grey    grey
137           9   Ag4   Al5   Ag6   Am8   Al20  1   grey    grey    grey    grey
138           9   Am4   Av5  Ato6   Am8  Ase20  2   grey    grey    grey    grey
139           9 Aspp4  Ate5  Ate6  Ate8 Aspp20  1   grey    grey    grey    grey
140           9   Av4   Av5   Av6   Av8  Ase20  2   grey    grey    grey    grey
141           9   Ac4   Av5   Ac6   Ac8   Ac20  7   grey    grey    grey    grey
142           9   Al4   Al5   Ac6   Ac8   Ac20  1   grey    grey    grey    grey
143           9   An4 Aspp5   Ac6   Ac8   Ac20  4   grey    grey    grey    grey
144           9   An4   An5   An6   An8  Ase20  2   grey    grey    grey    grey
145           9 Aspp4 Aspp5   An6  Ase8  Ase20  2   grey    grey    grey    grey
146           9   Ac4 Aspp5   Ac6   Ac8   Ac20  2   grey    grey    grey    grey
147           9   Ac4  Ate5  Ato6   Av8 Aspp20  1   grey    grey    grey    grey
148          10   Ag4   Ag5   Ag6   Ag8        10  black   black   black   black
149          10   Al4   Al5   Al6   Al8         1  black   black   black   black
150          10   An4   An5   An6   An8         4  black   black   black   black
151          10   Al4   Ag5   Al6   Al8         1  black   black   black   black
152          10   Al4 Aspp5 Aspp6   Al8         1  black   black   black   black
153          10   Ag4   Ag5   Ac6   Ac8         1  black   black   black   black
154          10   Ag4   Ag5   Ag6   Ac8        20  black   black   black   black
155          10   Ag4   Ag5   Am6   Am8         2  black   black   black   black
156          10   Ag4   Ag5   Ag6   Am8         1  black   black   black   black
157          10   An4   Ag5   An6   An8         1  black   black   black   black
158          10   Ag4   Ag5   An6   An8         4  black   black   black   black
159          10   Ag4   Ag5   Ag6   Ar8         3  black   black   black   black
160          10  Ato4  Ato5  Ato6  Ase8         1  black   black   black   black
161          10  Ato4 Aspp5  Ato6  Ase8         1  black   black   black   black
162          10  Ato4   Ag5 Aspp6 Aspp8         1  black   black   black   black
163          10  Ate4   Ag5 Aspp6 Aspp8         5  black   black   black   black
164          10   Ag4   Ag5   Av6   Av8         1  black   black   black   black
165          10   Ag4   Ag5   Ag6   Av8         2  black   black   black   black
166          10   Ag4   Ag5   Ag6   An8        16  black   black   black   black
167          10   Ag4   Ag5   Ag6  Ase8        16  black   black   black   black
168          10   Ag4   Ag5   Ag6 Aspp8         8  black   black   black   black
color.4
89     blue
90     blue
91   purple
92   purple
93   purple
94   purple
95   purple
96   purple
97   purple
98   purple
99   purple
100  purple
101  purple
102  purple
103  purple
104  purple
105  purple
106  purple
107  purple
108  purple
109  purple
110  purple
111  purple
112  violet
113  violet
114  violet
115  violet
116  violet
117  violet
118  violet
119  violet
120  violet
121  violet
122    grey
123    grey
124    grey
125    grey
126    grey
127    grey
128    grey
129    grey
130    grey
131    grey
132    grey
133    grey
134    grey
135    grey
136    grey
137    grey
138    grey
139    grey
140    grey
141    grey
142    grey
143    grey
144    grey
145    grey
146    grey
147    grey
148   black
149   black
150   black
151   black
152   black
153   black
154   black
155   black
156   black
157   black
158   black
159   black
160   black
161   black
162   black
163   black
164   black
165   black
166   black
167   black
168   black

Here is the code

#spit out structure details
str(sank_test)
&#39;data.frame&#39;:	168 obs. of  12 variables:
$ GobySpecies: int  1 1 1 1 1 1 1 2 2 2 ...
$ X2014      : chr  &quot;An4&quot; &quot;Ate4&quot; &quot;Ate4&quot; &quot;Ate4&quot; ...
$ X2015      : chr  &quot;Ate5&quot; &quot;Ate5&quot; &quot;Ate5&quot; &quot;Ate5&quot; ...
$ X2016      : chr  &quot;An6&quot; &quot;Ate6&quot; &quot;Ate6&quot; &quot;Ate6&quot; ...
$ X2018      : chr  &quot;An8&quot; &quot;Ate8&quot; &quot;Av8&quot; &quot;Ac8&quot; ...
$ X2020      : chr  &quot;&quot; &quot;&quot; &quot;&quot; &quot;&quot; ...
$ n          : int  10 29 12 29 12 1 7 25 18 4 ...
$ color      : chr  &quot;red&quot; &quot;red&quot; &quot;red&quot; &quot;red&quot; ...
$ color.1    : chr  &quot;red&quot; &quot;red&quot; &quot;red&quot; &quot;red&quot; ...
$ color.2    : chr  &quot;red&quot; &quot;red&quot; &quot;red&quot; &quot;red&quot; ...
$ color.3    : chr  &quot;red&quot; &quot;red&quot; &quot;red&quot; &quot;red&quot; ...
$ color.4    : chr  &quot;red&quot; &quot;red&quot; &quot;red&quot; &quot;red&quot; ...
list.of.packages &lt;- c(&quot;ggplot2&quot;,&quot;plotly&quot;,&quot;dplyr&quot;)
new.packages &lt;- list.of.packages[!(list.of.packages %in% installed.packages()[,&quot;Package&quot;])]
if(length(new.packages)) install.packages(new.packages)
#load specific packages of interest
lapply(list.of.packages,library,character.only = TRUE)
nodes_test &lt;- data.frame(name = unique(c(as.character(sank_test$GobySpecies),
as.character(sank_test$X2014),
as.character(sank_test$X2015),
as.character(sank_test$X2016),
as.character(sank_test$X2018),
as.character(sank_test$X2020))))
# create links_test dataframe
links_test &lt;- data.frame(source = match(sank_test$GobySpecies, nodes_test$name) - 1,
target = match(sank_test$X2014, nodes_test$name) - 1,
value = sank_test$n,
stringsAsFactors = FALSE)
links_test &lt;- rbind(links_test,
data.frame(source = match(sank_test$X2014, nodes_test$name) - 1,
target = match(sank_test$X2015, nodes_test$name) - 1,
value = sank_test$n,
stringsAsFactors = FALSE))
links_test &lt;- rbind(links_test,
data.frame(source = match(sank_test$X2015, nodes_test$name) - 1,
target = match(sank_test$X2016, nodes_test$name) - 1,
value = sank_test$n,
stringsAsFactors = FALSE))
links_test &lt;- rbind(links_test,
data.frame(source = match(sank_test$X2016, nodes_test$name) - 1,
target = match(sank_test$X2018, nodes_test$name) - 1,
value = sank_test$n,
stringsAsFactors = FALSE))
links_test &lt;- rbind(links_test,
data.frame(source = match(sank_test$X2018, nodes_test$name) - 1,
target = match(sank_test$X2020, nodes_test$name) - 1,
value = sank_test$n,
stringsAsFactors = FALSE))
plot_ly(
type = &quot;sankey&quot;,
orientation = &quot;h&quot;,
node = list(pad = 15,
thickness = 20,
line = list(color = &quot;black&quot;, width = 0.5),
label = nodes_test$name,color=&quot;black&quot;),
link = list(source = links_test$source,
target = links_test$target,
value = links_test$value,color = c(sank_test$color,sank_test$color.1,sank_test$color.2,sank_test$color.3,sank_test$color.4)),
textfont = list(size = 10),
width = 1000,
height = 480
) 
# Code
#make node colors same as link colors
plot_ly(
type = &quot;sankey&quot;,
orientation = &quot;h&quot;,
node = list(pad = 15,
thickness = 20,
line = list(color = &quot;black&quot;, width = 0.5),
label = nodes_test$name,color=c(&quot;red&quot;,&quot;orange&quot;,&quot;yellow&quot;,&quot;green&quot;,&quot;cyan&quot;,&quot;blue&quot;,&quot;purple&quot;,&quot;violet&quot;,&quot;grey&quot;,&quot;black&quot;)),
link = list(source = links_test$source,
target = links_test$target,
value = links_test$value,color = c(sank_test$color,sank_test$color.1,sank_test$color.2,sank_test$color.3,sank_test$color.4)),
textfont = list(size = 10),
width = 1000,
height = 480
)

I figured out how to get rid of the loop, but now one species (2, color=orange) is no longer showing anything in the last 'column to the right in the figure. Here is what I troubleshooted:when generating the node file, the code automatically generated a node for the blanks in certain cells. That node was a blank label in the node dataframe (here in nodes_test dataframe). So I removed the specific blank label (row 46) in the nodes dataframe and ran the rest as usual.

Sankey plot with plotly in R: how do I get the plot to skip over the NAs and not try to plot dead ends of some nodes?
Here is the nodes file initially so you can see the blank node that it created and then I have the code to remove it. Note, I also added a line of code that renamed the column to 'name':

1           G. aoyagii
2         G. axillaris
3        G. bilineatus
4           G. brochus
5     G. erythrospilus
6        G. fuscoruber
7           G. histrio
8     G. oculolineatus
9  G. quinquestrigatus
10        G. rivulatus
11       A. nasuta2014
12       A. tenuis2014
13    A. millepora2014
14   A. torresiana2014
15         A. spp.2014
16     A. cerealis2014
17      A. loripes2014
18      A. rosaria2014
19    A. gemmifera2014
20       A. valida2014
21       A. tenuis2015
22       A. nasuta2015
23    A. millepora2015
24     A. cerealis2015
25       A. valida2015
26   A. torresiana2015
27         A. spp.2015
28    A. gemmifera2015
29      A. rosaria2015
30      A. loripes2015
31       A. nasuta2016
32       A. tenuis2016
33    A. millepora2016
34     A. cerealis2016
35    A. gemmifera2016
36      A. loripes2016
37       A. valida2016
38   A. torresiana2016
39         A. spp.2016
40      A. rosaria2016
41       A. nasuta2018
42       A. tenuis2018
43       A. valida2018
44     A. cerealis2018
45      A. loripes2018
46                    
47      A. rosaria2018
48    A. gemmifera2018
49         A. spp.2018
50       A. selago2018
51    A. millepora2018
52       A. nasuta2020
53     A. cerealis2020
54         A. spp.2020
55       A. selago2020
56      A. loripes2020
57      A. rosaria2020
58       A. tenuis2020
59       A. valida2020
60    A. millepora2020
61    A. gemmifera2020

here is the code for removing that node

#NOW WITH NA NODE REMOVED
#remove node that is NA
nodes_test = data.frame(nodes_test[-c(46),])
#rename column
colnames(nodes_test)[1]=&#39;name&#39;

答案1

得分: 0

我已经搞清楚了,所以我在这里发布,以防其他人有同样的问题。在生成节点文件时,代码会自动生成某些单元格中空白的节点。该节点是节点数据框中的一个空白标签(在这里是nodes_test数据框中的第46行)。因此,我删除了节点数据框中特定的空白标签(第46行),然后像往常一样运行了其余部分。

这是最初的节点文件,以便您可以看到它创建的空白节点,然后我有代码来删除它。请注意,我还添加了一行代码来将列重命名为'名称':

1           G. aoyagii
2         G. axillaris
3        G. bilineatus
4           G. brochus
5     G. erythrospilus
6        G. fuscoruber
7           G. histrio
8     G. oculolineatus
9  G. quinquestrigatus
10        G. rivulatus
11       A. nasuta2014
12       A. tenuis2014
13    A. millepora2014
14   A. torresiana2014
15         A. spp.2014
16     A. cerealis2014
17      A. loripes2014
18      A. rosaria2014
19    A. gemmifera2014
20       A. valida2014
21       A. tenuis2015
22       A. nasuta2015
23    A. millepora2015
24     A. cerealis2015
25       A. valida2015
26   A. torresiana2015
27         A. spp.2015
28    A. gemmifera2015
29      A. loripes2015
30      A. rosaria2015
31       A. nasuta2016
32       A. tenuis2016
33    A. millepora2016
34     A. cerealis2016
35    A. gemmifera2016
36      A. loripes2016
37       A. valida2016
38   A. torresiana2016
39         A. spp.2016
40      A. rosaria2016
41       A. nasuta2018
42       A. tenuis2018
43       A. valida2018
44     A. cerealis2018
45      A. loripes2018
46                    
47      A. rosaria2018
48    A. gemmifera2018
49         A. spp.2018
50       A. selago2018
51    A. millepora2018
52       A. nasuta2020
53     A. cerealis2020
54         A. spp.2020
55       A. selago2020
56      A. loripes2020
57      A. rosaria2020
58       A. tenuis2020
59       A. valida2020
60    A. millepora2020
61    A. gemmifera2020
#现在已删除NA节点
#删除NA节点
nodes_test = data.frame(nodes_test[-c(46),])
#重命名列
colnames(nodes_test)[1]='名称'
英文:

I figured it out, so I am posting here in case someone else has the same problem. When generating the node file, the code automatically generated a node for the blanks in certain cells. That node was a blank label in the node dataframe (here in nodes_test dataframe). So I removed the specific blank label (row 46) in the nodes dataframe and ran the rest as usual.

Sankey plot with plotly in R: how do I get the plot to skip over the NAs and not try to plot dead ends of some nodes?

Here is the nodes file initially so you can see the blank node that it created and then I have the code to remove it. Note, I also added a line of code that renamed the column to 'name':

1           G. aoyagii
2         G. axillaris
3        G. bilineatus
4           G. brochus
5     G. erythrospilus
6        G. fuscoruber
7           G. histrio
8     G. oculolineatus
9  G. quinquestrigatus
10        G. rivulatus
11       A. nasuta2014
12       A. tenuis2014
13    A. millepora2014
14   A. torresiana2014
15         A. spp.2014
16     A. cerealis2014
17      A. loripes2014
18      A. rosaria2014
19    A. gemmifera2014
20       A. valida2014
21       A. tenuis2015
22       A. nasuta2015
23    A. millepora2015
24     A. cerealis2015
25       A. valida2015
26   A. torresiana2015
27         A. spp.2015
28    A. gemmifera2015
29      A. rosaria2015
30      A. loripes2015
31       A. nasuta2016
32       A. tenuis2016
33    A. millepora2016
34     A. cerealis2016
35    A. gemmifera2016
36      A. loripes2016
37       A. valida2016
38   A. torresiana2016
39         A. spp.2016
40      A. rosaria2016
41       A. nasuta2018
42       A. tenuis2018
43       A. valida2018
44     A. cerealis2018
45      A. loripes2018
46                    
47      A. rosaria2018
48    A. gemmifera2018
49         A. spp.2018
50       A. selago2018
51    A. millepora2018
52       A. nasuta2020
53     A. cerealis2020
54         A. spp.2020
55       A. selago2020
56      A. loripes2020
57      A. rosaria2020
58       A. tenuis2020
59       A. valida2020
60    A. millepora2020
61    A. gemmifera2020
#NOW WITH NA NODE REMOVED
#remove node that is NA
nodes_test = data.frame(nodes_test[-c(46),])
#rename column
colnames(nodes_test)[1]=&#39;name&#39;

huangapple
  • 本文由 发表于 2023年5月11日 06:48:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定