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评论96阅读模式
英文:

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?

问题

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

  1. #输出结构细节
  2. str(sank_test)
  3. 'data.frame': 168 obs. of 12 variables:
  4. $ GobySpecies: int 1 1 1 1 1 1 1 2 2 2 ...
  5. $ X2014 : chr "An4" "Ate4" "Ate4" "Ate4" ...
  6. $ X2015 : chr "Ate5" "Ate5" "Ate5" "Ate5" ...
  7. $ X2016 : chr "An6" "Ate6" "Ate6" "Ate6" ...
  8. $ X2018 : chr "An8" "Ate8" "Av8" "Ac8" ...
  9. $ X2020 : chr "" "" "" "" ...
  10. $ n : int 10 29 12 29 12 1 7 25 18 4 ...
  11. $ color : chr "red" "red" "red" "red" ...
  12. $ color.1 : chr "red" "red" "red" "red" ...
  13. $ color.2 : chr "red" "red" "red" "red" ...
  14. $ color.3 : chr "red" "red" "red" "red" ...
  15. $ color.4 : chr "red" "red" "red" "red" ...
  16. list.of.packages <- c("ggplot2","plotly","dplyr")
  17. new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
  18. if(length(new.packages)) install.packages(new.packages)
  19. #load specific packages of interest
  20. lapply(list.of.packages,library,character.only = TRUE)
  21. nodes_test <- data.frame(name = unique(c(as.character(sank_test$GobySpecies),
  22. as.character(sank_test$X2014),
  23. as.character(sank_test$X2015),
  24. as.character(sank_test$X2016),
  25. as.character(sank_test$X2018),
  26. as.character(sank_test$X2020))))
  27. # create links_test dataframe
  28. links_test <- data.frame(source = match(sank_test$GobySpecies, nodes_test$name) - 1,
  29. target = match(sank_test$X2014, nodes_test$name) - 1,
  30. value = sank_test$n,
  31. stringsAsFactors = FALSE)
  32. links_test <- rbind(links_test,
  33. data.frame(source = match(sank_test$X2014, nodes_test$name) - 1,
  34. target = match(sank_test$X2015, nodes_test$name) - 1,
  35. value = sank_test$n,
  36. stringsAsFactors = FALSE))
  37. links_test <- rbind(links_test,
  38. data.frame(source = match(sank_test$X2015, nodes_test$name) - 1,
  39. target = match(sank_test$X2016, nodes_test$name) - 1,
  40. value = sank_test$n,
  41. stringsAsFactors = FALSE))
  42. links_test <- rbind(links_test,
  43. data.frame(source = match(sank_test$X2016, nodes_test$name) - 1,
  44. target = match(sank_test$X2018, nodes_test$name) - 1,
  45. value = sank_test$n,
  46. stringsAsFactors = FALSE))
  47. links_test <- rbind(links_test,
  48. data.frame(source = match(sank_test$X2018, nodes_test$name) - 1,
  49. target = match(sank_test$X2020, nodes_test$name) - 1,
  50. value = sank_test$n,
  51. stringsAsFactors = FALSE))
  52. plot_ly(
  53. type = "sankey",
  54. orientation = "h",
  55. node = list(pad = 15,
  56. thickness = 20,
  57. line = list(color = "black", width = 0.5),
  58. label = nodes_test$name,color="black"),
  59. link = list(source = links_test$source,
  60. target = links_test$target,
  61. 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)),
  62. textfont = list(size = 10),
  63. width = 1000,
  64. height = 480
  65. )
  66. # Code
  67. #make node colors same as link colors
  68. plot_ly(
  69. type = "sankey",
  70. 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:

  1. head(sank_test,100)
  2. GobySpecies X2014 X2015 X2016 X2018 X2020 n color color.1 color.2 color.3
  3. 1 1 An4 Ate5 An6 An8 10 red red red red
  4. 2 1 Ate4 Ate5 Ate6 Ate8 29 red red red red
  5. 3 1 Ate4 Ate5 Ate6 Av8 12 red red red red
  6. 4 1 Ate4 Ate5 Ate6 Ac8 29 red red red red
  7. 5 1 Ate4 Ate5 Ate6 Al8 12 red red red red
  8. 6 1 Ate4 Ate5 An6 An8 1 red red red red
  9. 7 1 Ate4 Ate5 Ate6 An8 7 red red red red
  10. 8 2 An4 An5 An6 An20 25 orange orange orange orange
  11. 9 2 Am4 Am5 Am6 Ac20 18 orange orange orange orange
  12. 10 2 Am4 An5 Am6 Aspp20 4 orange orange orange orange
  13. 11 2 An4 Ac5 Ac6 Ac20 6 orange orange orange orange
  14. 12 2 An4 An5 Ac6 Ac20 5 orange orange orange orange
  15. 13 2 An4 An5 Ag6 Ase20 2 orange orange orange orange
  16. 14 2 An4 An5 Ag6 Ac20 20 orange orange orange orange
  17. 15 2 An4 An5 Al6 Al20 2 orange orange orange orange
  18. 16 2 An4 An5 Al6 Ac20 4 orange orange orange orange
  19. 17 2 An4 Av5 Av6 Ac20 6 orange orange orange orange
  20. 18 2 An4 An5 An6 Ac20 8 orange orange orange orange
  21. 19 3 Am4 Am5 Am6 6 yellow yellow yellow yellow
  22. 20 3 Ato4 Ato5 Ato6 5 yellow yellow yellow yellow
  23. 21 3 Aspp4 Aspp5 Aspp6 12 yellow yellow yellow yellow
  24. 22 3 Am4 Am5 Ag6 7 yellow yellow yellow yellow
  25. 23 3 Ato4 Am5 Ato6 3 yellow yellow yellow yellow
  26. 24 3 Aspp4 Aspp5 Ato6 1 yellow yellow yellow yellow
  27. 25 3 Ato4 Aspp5 Ato6 8 yellow yellow yellow yellow
  28. 26 3 Ato4 An5 An6 29 yellow yellow yellow yellow
  29. 27 3 Ato4 Ag5 Ag6 13 yellow yellow yellow yellow
  30. 28 3 Ato4 Ar5 Ag6 3 yellow yellow yellow yellow
  31. 29 3 Ato4 Ate5 Ag6 5 yellow yellow yellow yellow
  32. 30 3 Ato4 Av5 Ag6 1 yellow yellow yellow yellow
  33. 31 3 Ato4 Av5 Ato6 2 yellow yellow yellow yellow
  34. 32 3 Ato4 Ac5 Ato6 5 yellow yellow yellow yellow
  35. 33 4 Ac4 Ac5 Ac6 Ac8 Ac20 1 green green green green
  36. 34 4 Al4 Al5 Al6 Al8 Al20 7 green green green green
  37. 35 4 Ar4 Ar5 Ar6 Ar8 Ar20 2 green green green green
  38. 36 4 Ate4 Ate5 Ate6 Ate8 Ate20 3 green green green green
  39. 37 4 An4 An5 An6 An8 Ac20 5 green green green green
  40. 38 4 Al4 Av5 Av6 Av8 Av20 9 green green green green
  41. 39 4 Al4 Al5 Al6 Ag8 Ac20 10 green green green green
  42. 40 4 Ate4 Ate5 Ato6 Ate8 Ate20 1 green green green green
  43. 41 4 Aspp4 Ar5 Al6 Ate8 Aspp20 3 green green green green
  44. 42 4 Ar4 Ar5 Av6 Ar8 Av20 1 green green green green
  45. 43 4 Ac4 Al5 Al6 Ac8 Ac20 2 green green green green
  46. 44 4 An4 An5 Al6 An8 Ac20 4 green green green green
  47. 45 4 Ate4 Ate5 Al6 Ate8 Ate20 5 green green green green
  48. 46 4 Al4 Al5 Al6 Al8 Ac20 2 green green green green
  49. 47 4 An4 Al5 Al6 Ar8 Aspp20 6 green green green green
  50. 48 4 Ate4 Av5 Av6 Ate8 Ase20 2 green green green green
  51. 49 4 Al4 Al5 Al6 Ac8 Ac20 6 green green green green
  52. 50 4 An4 Al5 Al6 Ac8 Ac20 2 green green green green
  53. 51 4 Ate4 Ar5 Al6 Ate8 Av20 13 green green green green
  54. 52 4 Ate4 Av5 Al6 Ac8 Av20 5 green green green green
  55. 53 4 An4 Al5 Al6 Ac8 Aspp20 6 green green green green
  56. 54 4 Ate4 Av5 Al6 Ac8 Aspp20 3 green green green green
  57. 55 4 Ate4 Al5 Al6 Ac8 Aspp20 2 green green green green
  58. 56 5 Ac4 Ac5 Ac6 Ac8 Ac20 5 cyan cyan cyan cyan
  59. 57 5 An4 An5 An6 An8 An20 10 cyan cyan cyan cyan
  60. 58 5 Ate4 Ate5 Ate6 Ate8 Ate20 1 cyan cyan cyan cyan
  61. 59 5 Ag4 Ag5 Ag6 Ac8 Ac20 1 cyan cyan cyan cyan
  62. 60 5 Ag4 Ag5 Ac6 Ac8 Ac20 2 cyan cyan cyan cyan
  63. 61 5 Ate4 Ate5 Ate6 Ate8 Ac20 2 cyan cyan cyan cyan
  64. 62 5 Ate4 Ate5 Al6 Ate8 Ac20 4 cyan cyan cyan cyan
  65. 63 5 Al4 Al5 Al6 Ac8 Al20 1 cyan cyan cyan cyan
  66. 64 5 Al4 Al5 Al6 Ac8 Ac20 1 cyan cyan cyan cyan
  67. 65 5 Ate4 Am5 An6 Ate8 Ac20 4 cyan cyan cyan cyan
  68. 66 5 Aspp4 Av5 Av6 Aspp8 Aspp20 1 cyan cyan cyan cyan
  69. 67 5 Aspp4 Al5 Al6 Aspp8 Aspp20 2 cyan cyan cyan cyan
  70. 68 5 Am4 Am5 Am6 Ate8 Ac20 32 cyan cyan cyan cyan
  71. 69 5 An4 An5 An6 Ac8 An20 10 cyan cyan cyan cyan
  72. 70 5 An4 An5 An6 Ac8 Ac20 11 cyan cyan cyan cyan
  73. 71 5 An4 An5 Av6 Ase8 Ase20 1 cyan cyan cyan cyan
  74. 72 5 An4 An5 Av6 Ase8 Ar20 1 cyan cyan cyan cyan
  75. 73 5 An4 An5 Av6 Ase8 Ac20 3 cyan cyan cyan cyan
  76. 74 5 An4 Ac5 Al6 Ac8 Ac20 2 cyan cyan cyan cyan
  77. 75 5 An4 An5 Al6 Ate8 Ac20 1 cyan cyan cyan cyan
  78. 76 5 An4 An5 Ac6 Ate8 Ac20 3 cyan cyan cyan cyan
  79. 77 5 Am4 Am5 Av6 Aspp8 Ac20 1 cyan cyan cyan cyan
  80. 78 5 Am4 Am5 Aspp6 Aspp8 Ac20 1 cyan cyan cyan cyan
  81. 79 6 Am4 Am5 Am6 50 blue blue blue blue
  82. 80 6 An4 An5 An6 14 blue blue blue blue
  83. 81 6 Ag4 Ag5 Am6 4 blue blue blue blue
  84. 82 6 Ac4 Am5 Am6 4 blue blue blue blue
  85. 83 6 Aspp4 Am5 Am6 4 blue blue blue blue
  86. color.4
  87. 1 red
  88. 2 red
  89. 3 red
  90. 4 red
  91. 5 red
  92. 6 red
  93. 7 red
  94. 8 orange
  95. 9 orange
  96. 10 orange
  97. 11 orange
  98. 12 orange
  99. 13 orange
  100. 14 orange
  101. 15 orange
  102. 16 orange
  103. 17 orange
  104. 18 orange
  105. 19 yellow
  106. 20 yellow
  107. 21 yellow
  108. 22 yellow
  109. 23 yellow
  110. 24 yellow
  111. 25 yellow
  112. 26 yellow
  113. 27 yellow
  114. 28 yellow
  115. 29 yellow
  116. 30 yellow
  117. 31 yellow
  118. 32 yellow
  119. 33 green
  120. 34 green
  121. 35 green
  122. 36 green
  123. 37 green
  124. 38 green
  125. 39 green
  126. 40 green
  127. 41 green
  128. 42 green
  129. 43 green
  130. 44 green
  131. 45 green
  132. 46 green
  133. 47 green
  134. 48 green
  135. 49 green
  136. 50 green
  137. 51 green
  138. 52 green
  139. 53 green
  140. 54 green
  141. 55 green
  142. 56 cyan
  143. 57 cyan
  144. 58 cyan
  145. 59 cyan
  146. 60 cyan
  147. 61 cyan
  148. 62 cyan
  149. 63 cyan
  150. 64 cyan
  151. 65 cyan
  152. 66 cyan
  153. 67 cyan
  154. 68 cyan
  155. 69 cyan
  156. 70 cyan
  157. 71 cyan
  158. 72 cyan
  159. 73 cyan
  160. 74 cyan
  161. 75 cyan
  162. 76 cyan
  163. 77 cyan
  164. 78 cyan
  165. 79 blue
  166. 80 blue
  167. 81 blue
  168. 82 blue
  169. 83 blue
  170. tail(sank_test,80)
  171. GobySpecies X2014 X2015 X2016 X2018 X2020 n color color.1 color.2 color.3
  172. 89 6 Ag4 Av5 Av6 2 blue blue blue blue
  173. 90 6 Ag4 Al5 Ato6 2 blue blue blue blue
  174. 91 7 Ac4 Ac5 Ac6 Ac8 Ac20 8 purple purple purple purple
  175. 92 7 An4 An5 An6 Ac8 An20 10 purple purple purple purple
  176. 93 7 An4 An5 An6 Ac8 Ac20 42 purple purple purple purple
  177. 94 7 Ac4 Ar5 Ac6 Ate8 Ate20 1 purple purple purple purple
  178. 95 7 Ac4 Ato5 Ac6 Ate8 Ate20 1 purple purple purple purple
  179. 96 7 Al4 Al5 Al6 Av8 Al20 2 purple purple purple purple
  180. 97 7 Am4 Am5 Am6 Av8 Av20 2 purple purple purple purple
  181. 98 7 An4 Al5 Av6 Av8 Av20 2 purple purple purple purple
  182. 99 7 An4 Al5 Al6 Av8 Ac20 8 purple purple purple purple
  183. 100 7 Aspp4 An5 Am6 Ate8 Aspp20 2 purple purple purple purple
  184. 101 7 An4 Am5 Am6 Ate8 Al20 1 purple purple purple purple
  185. 102 7 An4 Am5 Am6 Ate8 Av20 1 purple purple purple purple
  186. 103 7 An4 Am5 Am6 Ate8 Aspp20 1 purple purple purple purple
  187. 104 7 An4 An5 Av6 Av8 Ac20 1 purple purple purple purple
  188. 105 7 An4 An5 Ac6 Ate8 Ac20 1 purple purple purple purple
  189. 106 7 An4 Ag5 Ag6 Ate8 Ac20 3 purple purple purple purple
  190. 107 7 An4 An5 Ate6 Ate8 Ac20 2 purple purple purple purple
  191. 108 7 An4 An5 An6 Av8 Ac20 1 purple purple purple purple
  192. 109 7 An4 An5 Am6 Ate8 Ac20 3 purple purple purple purple
  193. 110 7 An4 Am5 Am6 Av8 Ac20 4 purple purple purple purple
  194. 111 7 An4 Am5 Am6 Ate8 Ac20 4 purple purple purple purple
  195. 112 8 Al4 Al5 Al6 10 violet violet violet violet
  196. 113 8 Av4 Av5 Av6 12 violet violet violet violet
  197. 114 8 Ag4 Al5 Al6 7 violet violet violet violet
  198. 115 8 Ate4 Ar5 Al6 10 violet violet violet violet
  199. 116 8 Aspp4 Ar5 Al6 23 violet violet violet violet
  200. 117 8 Av4 Ar5 Av6 2 violet violet violet violet
  201. 118 8 An4 Al5 Al6 22 violet violet violet violet
  202. 119 8 An4 Al5 An6 1 violet violet violet violet
  203. 120 8 Av4 Ar5 Al6 12 violet violet violet violet
  204. 121 8 Av4 Al5 Al6 1 violet violet violet violet
  205. 122 9 Al4 Al5 Al6 Al8 Al20 1 grey grey grey grey
  206. 123 9 Am4 Am5 Am6 Am8 Am20 4 grey grey grey grey
  207. 124 9 An4 An5 An6 An8 An20 3 grey grey grey grey
  208. 125 9 Av4 Av5 Av6 Av8 Av20 3 grey grey grey grey
  209. 126 9 Aspp4 Aspp5 Aspp6 Aspp8 Aspp20 5 grey grey grey grey
  210. 127 9 An4 An5 An6 An8 Ac20 13 grey grey grey grey
  211. 128 9 An4 An5 An6 Ase8 Ase20 16 grey grey grey grey
  212. 129 9 Am4 Am5 Am6 Am8 Ase20 2 grey grey grey grey
  213. 130 9 Ate4 Ate5 Ate6 Ate8 Ac20 10 grey grey grey grey
  214. 131 9 Am4 Am5 Aspp6 Am8 Aspp20 6 grey grey grey grey
  215. 132 9 Ato4 Ato5 Ato6 Av8 Ase20 3 grey grey grey grey
  216. 133 9 Am4 Ato5 Ato6 Am8 Ase20 3 grey grey grey grey
  217. 134 9 Ag4 Ag5 Ag6 Ate8 Ag20 3 grey grey grey grey
  218. 135 9 Ag4 Ag5 Ag6 Al8 Al20 2 grey grey grey grey
  219. 136 9 Ag4 Al5 Ag6 Al8 Al20 1 grey grey grey grey
  220. 137 9 Ag4 Al5 Ag6 Am8 Al20 1 grey grey grey grey
  221. 138 9 Am4 Av5 Ato6 Am8 Ase20 2 grey grey grey grey
  222. 139 9 Aspp4 Ate5 Ate6 Ate8 Aspp20 1 grey grey grey grey
  223. 140 9 Av4 Av5 Av6 Av8 Ase20 2 grey grey grey grey
  224. 141 9 Ac4 Av5 Ac6 Ac8 Ac20 7 grey grey grey grey
  225. 142 9 Al4 Al5 Ac6 Ac8 Ac20 1 grey grey grey grey
  226. 143 9 An4 Aspp5 Ac6 Ac8 Ac20 4 grey grey grey grey
  227. 144 9 An4 An5 An6 An8 Ase20 2 grey grey grey grey
  228. 145 9 Aspp4 Aspp5 An6 Ase8 Ase20 2 grey grey grey grey
  229. 146 9 Ac4 Aspp5 Ac6 Ac8 Ac20 2 grey grey grey grey
  230. 147 9 Ac4 Ate5 Ato6 Av8 Aspp20 1 grey grey grey grey
  231. 148 10 Ag4 Ag5 Ag6 Ag8 10 black black black black
  232. 149 10 Al4 Al5 Al6 Al8 1 black black black black
  233. 150 10 An4 An5 An6 An8 4 black black black black
  234. 151 10 Al4 Ag5 Al6 Al8 1 black black black black
  235. 152 10 Al4 Aspp5 Aspp6 Al8 1 black black black black
  236. 153 10 Ag4 Ag5 Ac6 Ac8 1 black black black black
  237. 154 10 Ag4 Ag5 Ag6 Ac8 20 black black black black
  238. 155 10 Ag4 Ag5 Am6 Am8 2 black black black black
  239. 156 10 Ag4 Ag5 Ag6 Am8 1 black black black black
  240. 157 10 An4 Ag5 An6 An8 1 black black black black
  241. 158 10 Ag4 Ag5 An6 An8 4 black black black black
  242. 159 10 Ag4 Ag5 Ag6 Ar8 3 black black black black
  243. 160 10 Ato4 Ato5 Ato6 Ase8 1 black black black black
  244. 161 10 Ato4 Aspp5 Ato6 Ase8 1 black black black black
  245. 162 10 Ato4 Ag5 Aspp6 Aspp8 1 black black black black
  246. 163 10 Ate4 Ag5 Aspp6 Aspp8 5 black black black black
  247. 164 10 Ag4 Ag5 Av6 Av8 1 black black black black
  248. 165 10 Ag4 Ag5 Ag6 Av8 2 black black black black
  249. 166 10 Ag4 Ag5 Ag6 An8 16 black black black black
  250. 167 10 Ag4 Ag5 Ag6 Ase8 16 black black black black
  251. 168 10 Ag4 Ag5 Ag6 Aspp8 8 black black black black
  252. color.4
  253. 89 blue
  254. 90 blue
  255. 91 purple
  256. 92 purple
  257. 93 purple
  258. 94 purple
  259. 95 purple
  260. 96 purple
  261. 97 purple
  262. 98 purple
  263. 99 purple
  264. 100 purple
  265. 101 purple
  266. 102 purple
  267. 103 purple
  268. 104 purple
  269. 105 purple
  270. 106 purple
  271. 107 purple
  272. 108 purple
  273. 109 purple
  274. 110 purple
  275. 111 purple
  276. 112 violet
  277. 113 violet
  278. 114 violet
  279. 115 violet
  280. 116 violet
  281. 117 violet
  282. 118 violet
  283. 119 violet
  284. 120 violet
  285. 121 violet
  286. 122 grey
  287. 123 grey
  288. 124 grey
  289. 125 grey
  290. 126 grey
  291. 127 grey
  292. 128 grey
  293. 129 grey
  294. 130 grey
  295. 131 grey
  296. 132 grey
  297. 133 grey
  298. 134 grey
  299. 135 grey
  300. 136 grey
  301. 137 grey
  302. 138 grey
  303. 139 grey
  304. 140 grey
  305. 141 grey
  306. 142 grey
  307. 143 grey
  308. 144 grey
  309. 145 grey
  310. 146 grey
  311. 147 grey
  312. 148 black
  313. 149 black
  314. 150 black
  315. 151 black
  316. 152 black
  317. 153 black
  318. 154 black
  319. 155 black
  320. 156 black
  321. 157 black
  322. 158 black
  323. 159 black
  324. 160 black
  325. 161 black
  326. 162 black
  327. 163 black
  328. 164 black
  329. 165 black
  330. 166 black
  331. 167 black
  332. 168 black

Here is the code

  1. #spit out structure details
  2. str(sank_test)
  3. &#39;data.frame&#39;: 168 obs. of 12 variables:
  4. $ GobySpecies: int 1 1 1 1 1 1 1 2 2 2 ...
  5. $ X2014 : chr &quot;An4&quot; &quot;Ate4&quot; &quot;Ate4&quot; &quot;Ate4&quot; ...
  6. $ X2015 : chr &quot;Ate5&quot; &quot;Ate5&quot; &quot;Ate5&quot; &quot;Ate5&quot; ...
  7. $ X2016 : chr &quot;An6&quot; &quot;Ate6&quot; &quot;Ate6&quot; &quot;Ate6&quot; ...
  8. $ X2018 : chr &quot;An8&quot; &quot;Ate8&quot; &quot;Av8&quot; &quot;Ac8&quot; ...
  9. $ X2020 : chr &quot;&quot; &quot;&quot; &quot;&quot; &quot;&quot; ...
  10. $ n : int 10 29 12 29 12 1 7 25 18 4 ...
  11. $ color : chr &quot;red&quot; &quot;red&quot; &quot;red&quot; &quot;red&quot; ...
  12. $ color.1 : chr &quot;red&quot; &quot;red&quot; &quot;red&quot; &quot;red&quot; ...
  13. $ color.2 : chr &quot;red&quot; &quot;red&quot; &quot;red&quot; &quot;red&quot; ...
  14. $ color.3 : chr &quot;red&quot; &quot;red&quot; &quot;red&quot; &quot;red&quot; ...
  15. $ color.4 : chr &quot;red&quot; &quot;red&quot; &quot;red&quot; &quot;red&quot; ...
  16. list.of.packages &lt;- c(&quot;ggplot2&quot;,&quot;plotly&quot;,&quot;dplyr&quot;)
  17. new.packages &lt;- list.of.packages[!(list.of.packages %in% installed.packages()[,&quot;Package&quot;])]
  18. if(length(new.packages)) install.packages(new.packages)
  19. #load specific packages of interest
  20. lapply(list.of.packages,library,character.only = TRUE)
  21. nodes_test &lt;- data.frame(name = unique(c(as.character(sank_test$GobySpecies),
  22. as.character(sank_test$X2014),
  23. as.character(sank_test$X2015),
  24. as.character(sank_test$X2016),
  25. as.character(sank_test$X2018),
  26. as.character(sank_test$X2020))))
  27. # create links_test dataframe
  28. links_test &lt;- data.frame(source = match(sank_test$GobySpecies, nodes_test$name) - 1,
  29. target = match(sank_test$X2014, nodes_test$name) - 1,
  30. value = sank_test$n,
  31. stringsAsFactors = FALSE)
  32. links_test &lt;- rbind(links_test,
  33. data.frame(source = match(sank_test$X2014, nodes_test$name) - 1,
  34. target = match(sank_test$X2015, nodes_test$name) - 1,
  35. value = sank_test$n,
  36. stringsAsFactors = FALSE))
  37. links_test &lt;- rbind(links_test,
  38. data.frame(source = match(sank_test$X2015, nodes_test$name) - 1,
  39. target = match(sank_test$X2016, nodes_test$name) - 1,
  40. value = sank_test$n,
  41. stringsAsFactors = FALSE))
  42. links_test &lt;- rbind(links_test,
  43. data.frame(source = match(sank_test$X2016, nodes_test$name) - 1,
  44. target = match(sank_test$X2018, nodes_test$name) - 1,
  45. value = sank_test$n,
  46. stringsAsFactors = FALSE))
  47. links_test &lt;- rbind(links_test,
  48. data.frame(source = match(sank_test$X2018, nodes_test$name) - 1,
  49. target = match(sank_test$X2020, nodes_test$name) - 1,
  50. value = sank_test$n,
  51. stringsAsFactors = FALSE))
  52. plot_ly(
  53. type = &quot;sankey&quot;,
  54. orientation = &quot;h&quot;,
  55. node = list(pad = 15,
  56. thickness = 20,
  57. line = list(color = &quot;black&quot;, width = 0.5),
  58. label = nodes_test$name,color=&quot;black&quot;),
  59. link = list(source = links_test$source,
  60. target = links_test$target,
  61. 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)),
  62. textfont = list(size = 10),
  63. width = 1000,
  64. height = 480
  65. )
  66. # Code
  67. #make node colors same as link colors
  68. plot_ly(
  69. type = &quot;sankey&quot;,
  70. orientation = &quot;h&quot;,
  71. node = list(pad = 15,
  72. thickness = 20,
  73. line = list(color = &quot;black&quot;, width = 0.5),
  74. 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;)),
  75. link = list(source = links_test$source,
  76. target = links_test$target,
  77. 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)),
  78. textfont = list(size = 10),
  79. width = 1000,
  80. height = 480
  81. )

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

here is the code for removing that node

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

答案1

得分: 0

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

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

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

确定