创建一个三角形相关矩阵风格的图,用于物种两两比较。

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

Creating a Triangle Corrlation Matrix Style plot for Species Pairwise comparison

问题

我已经进行了一系列的两两卡方检验,得到了每对物种的p值数据框。我想使用ggplot创建一种类似于相关矩阵的可视化,以便我们可以看到哪些物种对具有显著的p值。对于大于0.05的值使用一种颜色,对于小于或等于0.05的值使用另一种颜色。类似于这个示例:(https://www.researchgate.net/publication/293654442_cooccur_Probabilistic_Species_Co-Occurrence_Analysis_in_R)。

我已经尝试了以下这些指南:

但没有太大的成功。我在以下两个方面遇到了问题:1)将其放入三角形中,2)将颜色设为二进制而不是渐变色。我希望保持物种的顺序与数据框中的顺序相同。任何建议都将非常有帮助。我一直在为此而烦恼。

我已经附上了数据框的dputs,分别是宽格式(Wide.df)和长格式(long.df)的数据。

英文:

I have been doing a series of pairwise chi-squared comparisons of species, and have a data frame with the p_values for each species pair. I would like to create a visualization using ggplot similar to a correlation matrix so we can see what species pairs have significant p-values. With values >.05 in one color and values < or = 0.5 in another. Something similar to this: (https://www.researchgate.net/publication/293654442_cooccur_Probabilistic_Species_Co-Occurrence_Analysis_in_R)

创建一个三角形相关矩阵风格的图,用于物种两两比较。

I have tried following these guide:

http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization

https://www.youtube.com/watch?v=E3De2A73ako

Without much success. I am having trouble 1) getting it into a triangle and 2) having the colors be binary rather than a gradient. I would prefer to maintain the order of the species as I have them in my data frames. Any advice would be amazing. I have been driving myself bandannas with this one.

I have attached dputs of data frames both in wide and long formats!

Thanks very much!

Wide.df:

  1. structure(list(Species1 = c(NA, 8.29661485364936e-14, 0.197328035330918,
  2. 3.73447143215061e-05, 0.0200245227839388, 0.000158518795659732,
  3. 0.999999999999999, 0.999999999999999, 0.200661174254572, 0.0259711510883781,
  4. 0.624415639985824, 0.999999999999995, 0.999999999999991), Species2 = c(8.29661485364936e-14,
  5. NA, 0.911548596172133, 0.0808672833723648, 0.00232408080140171,
  6. 0.00257625010714883, 0.999999999999777, 0.999999999999777, 0.789829339795786,
  7. 0.198077829941541, 0.768466049890891, 0.999999999999885, 0.999999999999994
  8. ), Species3 = c(0.197328035330918, 0.911548596172133, NA, 0.181696316252709,
  9. 0.575238254258972, 0.939866701307512, 0.999999999999999, 0.999999999999999,
  10. 0.999999999999999, 0.507611967012476, 0.673527222144056, 0.999999999999996,
  11. 1), Species4 = c(3.73447143215061e-05, 0.0808672833723648, 0.181696316252709,
  12. NA, 1.40996305374498e-10, 0.405410680293625, 0.999999999999999,
  13. 0.999999999999999, 1.56797538624063e-08, 8.50447159522988e-05,
  14. 0.00644405295214749, 0.88381428087806, 0.99999999999993), Species5 = c(0.0200245227839388,
  15. 0.00232408080140171, 0.575238254258972, 1.40996305374498e-10,
  16. NA, 0.622095128306733, 0.999999999999959, 0.999999999999959,
  17. 0.000115476294641169, 1.60499061530966e-19, 4.67520836455185e-05,
  18. 0.528580893876124, 0.795044191844885), Species6 = c(0.000158518795659732,
  19. 0.00257625010714883, 0.939866701307512, 0.405410680293625, 0.622095128306733,
  20. NA, 0.999999999999999, 0.999999999999999, 0.200661174254572,
  21. 0.410636112084533, 0.999999999999929, 0.999999999999995, 0.999999999999991
  22. ), Species7 = c(0.999999999999999, 0.999999999999777, 0.999999999999999,
  23. 0.999999999999999, 0.999999999999959, 0.999999999999999, NA,
  24. 0.999999999999299, 0.779426832974571, 0.999999999999933, 0.999999999999999,
  25. 0.999999999999998, 0.999999999999687), Species8 = c(0.999999999999999,
  26. 0.999999999999777, 0.999999999999999, 0.999999999999999, 0.999999999999959,
  27. 0.999999999999999, 0.999999999999299, NA, 0.999999999999999,
  28. 0.611136265859179, 0.999999999999999, 0.999999999999998, 0.999999999999687
  29. ), Species9 = c(0.200661174254572, 0.789829339795786, 0.999999999999999,
  30. 1.56797538624063e-08, 0.000115476294641169, 0.200661174254572,
  31. 0.779426832974571, 0.999999999999999, NA, 0.0311037604732729,
  32. 0.0122054515551129, 0.999999999999984, 0.999999999999999), Species10 = c(0.0259711510883781,
  33. 0.198077829941541, 0.507611967012476, 8.50447159522988e-05, 1.60499061530966e-19,
  34. 0.410636112084533, 0.999999999999933, 0.611136265859179, 0.0311037604732729,
  35. NA, 0.0403275386741277, 0.508244635418544, 0.999999999999999),
  36. Species11 = c(0.624415639985824, 0.768466049890891, 0.673527222144056,
  37. 0.00644405295214749, 4.67520836455185e-05, 0.999999999999929,
  38. 0.999999999999999, 0.999999999999999, 0.0122054515551129,
  39. 0.0403275386741277, NA, 1, 0.999999999999823), Species12 = c(0.999999999999995,
  40. 0.999999999999885, 0.999999999999996, 0.88381428087806, 0.528580893876124,
  41. 0.999999999999995, 0.999999999999998, 0.999999999999998,
  42. 0.999999999999984, 0.508244635418544, 1, NA, 0.999999999998991
  43. ), Species13 = c(0.999999999999991, 0.999999999999994, 1,
  44. 0.99999999999993, 0.795044191844885, 0.999999999999991, 0.999999999999687,
  45. 0.999999999999687, 0.999999999999999, 0.999999999999999,
  46. 0.999999999999823, 0.999999999998991, NA)), row.names = c(&quot;Species1&quot;,
  47. &quot;Species2&quot;, &quot;Species3&quot;, &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;,
  48. &quot;Species8&quot;, &quot;Species9&quot;, &quot;Species10&quot;, &quot;Species11&quot;, &quot;Species12&quot;,
  49. &quot;Species13&quot;), class = &quot;data.frame&quot;)

long.df:

  1. structure(list(SpeciesA = c(&quot;Species1&quot;, &quot;Species2&quot;, &quot;Species3&quot;,
  2. &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;, &quot;Species8&quot;, &quot;Species9&quot;,
  3. &quot;Species10&quot;, &quot;Species11&quot;, &quot;Species12&quot;, &quot;Species13&quot;, &quot;Species1&quot;,
  4. &quot;Species2&quot;, &quot;Species3&quot;, &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;,
  5. &quot;Species8&quot;, &quot;Species9&quot;, &quot;Species10&quot;, &quot;Species11&quot;, &quot;Species12&quot;,
  6. &quot;Species13&quot;, &quot;Species1&quot;, &quot;Species2&quot;, &quot;Species3&quot;, &quot;Species4&quot;,
  7. &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;, &quot;Species8&quot;, &quot;Species9&quot;, &quot;Species10&quot;,
  8. &quot;Species11&quot;, &quot;Species12&quot;, &quot;Species13&quot;, &quot;Species1&quot;, &quot;Species2&quot;,
  9. &quot;Species3&quot;, &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;, &quot;Species8&quot;,
  10. &quot;Species9&quot;, &quot;Species10&quot;, &quot;Species11&quot;, &quot;Species12&quot;, &quot;Species13&quot;,
  11. &quot;Species1&quot;, &quot;Species2&quot;, &quot;Species3&quot;, &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;,
  12. &quot;Species7&quot;, &quot;Species8&quot;, &quot;Species9&quot;, &quot;Species10&quot;, &quot;Species11&quot;,
  13. &quot;Species12&quot;, &quot;Species13&quot;, &quot;Species1&quot;, &quot;Species2&quot;, &quot;Species3&quot;,
  14. &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;, &quot;Species8&quot;, &quot;Species9&quot;,
  15. &quot;Species10&quot;, &quot;Species11&quot;, &quot;Species12&quot;, &quot;Species13&quot;, &quot;Species1&quot;,
  16. &quot;Species2&quot;, &quot;Species3&quot;, &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;,
  17. &quot;Species8&quot;, &quot;Species9&quot;, &quot;Species10&quot;, &quot;Species11&quot;, &quot;Species12&quot;,
  18. &quot;Species13&quot;, &quot;Species1&quot;, &quot;Species2&quot;, &quot;Species3&quot;, &quot;Species4&quot;,
  19. &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;, &quot;Species8&quot;, &quot;Species9&quot;, &quot;Species10&quot;,
  20. &quot;Species11&quot;, &quot;Species12&quot;, &quot;Species13&quot;, &quot;Species1&quot;, &quot;Species2&quot;,
  21. &quot;Species3&quot;, &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;, &quot;Species8&quot;,
  22. &quot;Species9&quot;, &quot;Species10&quot;, &quot;Species11&quot;, &quot;Species12&quot;, &quot;Species13&quot;,
  23. &quot;Species1&quot;, &quot;Species2&quot;, &quot;Species3&quot;, &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;,
  24. &quot;Species7&quot;, &quot;Species8&quot;, &quot;Species9&quot;, &quot;Species10&quot;, &quot;Species11&quot;,
  25. &quot;Species12&quot;, &quot;Species13&quot;, &quot;Species1&quot;, &quot;Species2&quot;, &quot;Species3&quot;,
  26. &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;, &quot;Species8&quot;, &quot;Species9&quot;,
  27. &quot;Species10&quot;, &quot;Species11&quot;, &quot;Species12&quot;, &quot;Species13&quot;, &quot;Species1&quot;,
  28. &quot;Species2&quot;, &quot;Species3&quot;, &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;,
  29. &quot;Species8&quot;, &quot;Species9&quot;, &quot;Species10&quot;, &quot;Species11&quot;, &quot;Species12&quot;,
  30. &quot;Species13&quot;, &quot;Species1&quot;, &quot;Species2&quot;, &quot;Species3&quot;, &quot;Species4&quot;,
  31. &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;, &quot;Species8&quot;, &quot;Species9&quot;, &quot;Species10&quot;,
  32. &quot;Species11&quot;, &quot;Species12&quot;, &quot;Species13&quot;), SpeciesB = structure(c(1L,
  33. 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
  34. 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
  35. 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
  36. 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
  37. 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L,
  38. 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
  39. 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
  40. 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
  41. 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
  42. 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
  43. 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
  44. 13L, 13L, 13L, 13L), .Label = c(&quot;Species1&quot;, &quot;Species2&quot;, &quot;Species3&quot;,
  45. &quot;Species4&quot;, &quot;Species5&quot;, &quot;Species6&quot;, &quot;Species7&quot;, &quot;Species8&quot;, &quot;Species9&quot;,
  46. &quot;Species10&quot;, &quot;Species11&quot;, &quot;Species12&quot;, &quot;Species13&quot;), class = &quot;factor&quot;),
  47. p_value = c(NA, 8.29661485364936e-14, 0.197328035330918,
  48. 3.73447143215061e-05, 0.0200245227839388, 0.000158518795659732,
  49. 0.999999999999999, 0.999999999999999, 0.200661174254572,
  50. 0.0259711510883781, 0.624415639985824, 0.999999999999995,
  51. 0.999999999999991, 8.29661485364936e-14, NA, 0.911548596172133,
  52. 0.0808672833723648, 0.00232408080140171, 0.00257625010714883,
  53. 0.999999999999777, 0.999999999999777, 0.789829339795786,
  54. 0.198077829941541, 0.768466049890891, 0.999999999999885,
  55. 0.999999999999994, 0.197328035330918, 0.911548596172133,
  56. NA, 0.181696316252709, 0.575238254258972, 0.939866701307512,
  57. 0.999999999999999, 0.999999999999999, 0.999999999999999,
  58. 0.507611967012476, 0.673527222144056, 0.999999999999996,
  59. 1, 3.73447143215061e-05, 0.0808672833723648, 0.181696316252709,
  60. NA, 1.40996305374498e-10, 0.405410680293625, 0.999999999999999,
  61. 0.999999999999999, 1.56797538624063e-08, 8.50447159522988e-05,
  62. 0.00644405295214749, 0.88381428087806, 0.99999999999993,
  63. 0.0200245227839388, 0.00232408080140171, 0.575238254258972,
  64. 1.40996305374498e-10, NA, 0.622095128306733, 0.999999999999959,
  65. 0.999999999999959, 0.000115476294641169, 1.60499061530966e-19,
  66. 4.67520836455185e-05, 0.528580893876124, 0.795044191844885,
  67. 0.000158518795659732, 0.00257625010714883, 0.939866701307512,
  68. 0.405410680293625, 0.622095128306733, NA, 0.999999999999999,
  69. 0.999999999999999, 0.200661174254572, 0.410636112084533,
  70. 0.999999999999929, 0.999999999999995, 0.999999999999991,
  71. 0.999999999999999, 0.999999999999777, 0.999999999999999,
  72. 0.999999999999999, 0.999999999999959, 0.999999999999999,
  73. NA, 0.999999999999299, 0.779426832974571, 0.999999999999933,
  74. 0.999999999999999, 0.999999999999998, 0.999999999999687,
  75. 0.999999999999999, 0.999999999999777, 0.999999999999999,
  76. 0.999999999999999, 0.999999999999959, 0.999999999999999,
  77. 0.999999999999299, NA, 0.999999999999999, 0.611136265859179,
  78. 0.999999999999999, 0.999999999999998, 0.999999999999687,
  79. 0.200661174254572, 0.789829339795786, 0.999999999999999,
  80. 1.56797538624063e-08, 0.000115476294641169, 0.200661174254572,
  81. 0.779426832974571, 0.999999999999999, NA, 0.0311037604732729,
  82. 0.0122054515551129, 0.999999999999984, 0.999999999999999,
  83. 0.0259711510883781, 0.198077829941541, 0.507611967012476,
  84. 8.50447159522988e-05, 1.60499061530966e-19, 0.410636112084533,
  85. 0.999999999999933, 0.611136265859179, 0.0311037604732729,
  86. NA, 0.0403275386741277, 0.508244635418544, 0.999999999999999,
  87. 0.624415639985824, 0.768466049890891, 0.673527222144056,
  88. 0.00644405295214749, 4.67520836455185e-05, 0.999999999999929,
  89. 0.999999999999999, 0.999999999999999, 0.0122054515551129,
  90. 0.0403275386741277, NA, 1, 0.999999999999823, 0.999999999999995,
  91. 0.999999999999885, 0.999999999999996, 0.88381428087806, 0.528580893876124,
  92. 0.999999999999995, 0.999999999999998, 0.999999999999998,
  93. 0.999999999999984, 0.508244635418544, 1, NA, 0.999999999998991,
  94. 0.999999999999991, 0.999999999999994, 1, 0.99999999999993,
  95. 0.795044191844885, 0.999999999999991, 0.999999999999687,
  96. 0.999999999999687, 0.999999999999999, 0.999999999999999,
  97. 0.999999999999823, 0.999999999998991, NA)), row.names = c(NA,
  98. -169L), class = &quot;data.frame&quot;)

答案1

得分: 1

以下是翻译好的部分:

  • 使用 cut 将数据分组成简单的分组热图,但需要经过几个阶段:

  • 使用 cut 对数据进行分组;我随意将它们分成了0、0.25、0.5和1的分组,您可以根据自己的需要自由更改这些数字;

  • 为了根据类似数字排序的方式保留物种的顺序,我们需要将它们转换为 factor,为了正确排序它们,我们需要解析出数字,因此需要使用 as.integer(gsub(..)) 然后稍后使用 factor

  • 为了获得半三角形,我确保 x 轴上的 num* 值大于 y 轴上的值;这是任意的,但与您的示例图匹配。

  • 为了在对角线上显示标签,我们保留了进入 ggplot(.) 的数据中的 numA == numB,然后将对角线用于瓷砖,然后仅保留标签的对角线部分(geom_text)。

  • 为了扩展画布以容纳对角线的标签,我们需要同时使用 expand= 扩展两个轴;实际的数字可能取决于画布(pdf、html等)。

我使用了基本的 R(R-4)转换/子集,这很容易转换为使用 dplyr 的 mutate/filter

创建一个三角形相关矩阵风格的图,用于物种两两比较。

英文:

Here's a crude start. The basic plot is a simple binned heatmap, but there are several stages we need to go through:

  • bin the data with cut; I arbitrarily cut them into bins of 0, 0.25, 0.5, and 1, feel free to change the numbers for your own purpose;
  • to preserve order based on the numeric-like sorting of the species, we need to convert them to factors, and to order them right we need to parse out the numbers, ergo the as.integer(gsub(..)) then factor later.
  • to get a half-triangle, I make sure that the x-axis num* value is greater than the y-axis; this is arbitrary but matched your sample plot.
  • in order to show the labels on the diagonal, though, we keep numA == numB in the data that goes into ggplot(.), subset out the diagonal for the tiles, then keep only the diagonal for the labels (geom_text).
  • to expand the canvas a little to allow for the diagonal's labels, we need to expand= both axes; the actual numbers may depend on the canvas (pdf, html, etc).

I used base-R (R-4) transform/subset, this is easily converted to use dplyr's mutate/filter.

  1. long.df |&gt;
  2. transform(
  3. bin = cut(p_value, c(0, 0.25, 0.5, 1), labels = c(&quot;low&quot;, &quot;moderate&quot;, &quot;high&quot;)),
  4. numA = as.integer(gsub(&quot;\\D&quot;, &quot;&quot;, SpeciesA)),
  5. numB = as.integer(gsub(&quot;\\D&quot;, &quot;&quot;, SpeciesB))
  6. ) |&gt; transform(
  7. SpeciesA = factor(SpeciesA, levels = unique(SpeciesA[order(numA)])),
  8. SpeciesB = factor(SpeciesB, levels = unique(SpeciesB[order(numB)]))
  9. ) |&gt;
  10. subset(numA &gt;= numB) |&gt;
  11. ggplot(aes(SpeciesA, SpeciesB)) +
  12. geom_tile(aes(fill = bin), data = ~ subset(., numA &gt; numB)) +
  13. geom_text(aes(label = SpeciesA), data = ~ subset(., numA == numB),
  14. hjust = 1.1, angle = -30) +
  15. scale_x_discrete(name = NULL, expand = expansion(0, c(2.5, 0)), drop = FALSE) +
  16. scale_y_discrete(name = NULL, expand = expansion(0, c(0, 2)), drop = FALSE) +
  17. theme(
  18. axis.text = element_blank(),
  19. axis.line = element_blank(),
  20. axis.ticks = element_blank()
  21. )

创建一个三角形相关矩阵风格的图,用于物种两两比较。

答案2

得分: 1

这段代码是使用r2evans的帮助解决的!我最终使用的代码如下:

  1. test.plot1 <- long.df1 %>%
  2. mutate(
  3. color = ifelse(test = p_value > 0.05,
  4. yes = "#440154FF",
  5. no = (ifelse(test = p_value <= 0.05,
  6. yes = "#FDE725FF",
  7. no = NA)
  8. )
  9. )
  10. ) %>%
  11. mutate(
  12. Species1 = factor(Species1, levels = unique(Species1[order(order_species1)])),
  13. Species2 = factor(Species2, levels = unique(Species2[order(order_species2)]))
  14. ) %>%
  15. subset(order_species1 >= order_species2) %>%
  16. ggplot(aes(Species1, Species2)) +
  17. geom_tile(aes(fill = color), data = ~ subset(., order_species1 > order_species2), color = "white") +
  18. geom_text(aes(label = Species1), data = ~ subset(., order_species1 == order_species2),
  19. hjust = 1.1, angle = -30) +
  20. scale_x_discrete(name = NULL, expand = expansion(0, c(2.5, 0)), drop = FALSE) +
  21. scale_y_discrete(name = NULL, expand = expansion(0, c(0, 2)), drop = FALSE) +
  22. theme_minimal() +
  23. theme(
  24. axis.text = element_blank(),
  25. axis.line = element_blank(),
  26. axis.ticks = element_blank(),
  27. panel.grid = element_blank()
  28. ) + scale_fill_identity()
  29. test.plot1

这段代码生成了上述图片中的图表。

英文:

Solved with the help of r2evans! The code I ended up using is below!

  1. test.plot1 &lt;- long.df1 %&gt;%
  2. mutate(
  3. color = # get colors of graphs
  4. ifelse(test = p_value &gt; 0.05,
  5. yes = &quot;#440154FF&quot;,
  6. no = (ifelse(test = p_value &lt;= 0.05,
  7. yes = &quot;#FDE725FF&quot;,
  8. no = NA)
  9. )
  10. )
  11. ) %&gt;%
  12. mutate(
  13. Species1 = factor(Species1, levels = unique(Species1[order(order_species1)])),
  14. Species2 = factor(Species2 , levels = unique(Species2 [order(order_species2)]))
  15. ) %&gt;%
  16. subset(order_species1&gt;=order_species2) %&gt;%
  17. ggplot(aes(Species1, Species2)) +
  18. geom_tile(aes(fill = color), data = ~ subset(., order_species1&gt; order_species2), color = &quot;white&quot;) +
  19. geom_text(aes(label = Species1), data = ~ subset(., order_species1 == order_species2),
  20. hjust = 1.1, angle = -30) +
  21. scale_x_discrete(name = NULL, expand = expansion(0, c(2.5, 0)), drop = FALSE) +
  22. scale_y_discrete(name = NULL, expand = expansion(0, c(0, 2)), drop = FALSE) +
  23. theme_minimal() +
  24. theme(
  25. axis.text = element_blank(),
  26. axis.line = element_blank(),
  27. axis.ticks = element_blank(),
  28. panel.grid = element_blank()
  29. ) + scale_fill_identity()
  30. test.plot1

That produces this plot:
创建一个三角形相关矩阵风格的图,用于物种两两比较。

Thanks all!

Dput for data frame is here:

  1. structure(list(Species1 = c(&quot;SpeciesA&quot;, &quot;SpeciesB&quot;, &quot;SpeciesC&quot;,
  2. &quot;SpeciesD&quot;, &quot;SpeciesE&quot;, &quot;SpeciesF&quot;, &quot;SpeciesG&quot;, &quot;SpeciesH&quot;, &quot;SpeciesI&quot;,
  3. &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;, &quot;SpeciesL&quot;, &quot;SpeciesM&quot;, &quot;SpeciesA&quot;, &quot;SpeciesB&quot;,
  4. &quot;SpeciesC&quot;, &quot;SpeciesD&quot;, &quot;SpeciesE&quot;, &quot;SpeciesF&quot;, &quot;SpeciesG&quot;, &quot;SpeciesH&quot;,
  5. &quot;SpeciesI&quot;, &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;, &quot;SpeciesL&quot;, &quot;SpeciesM&quot;, &quot;SpeciesA&quot;,
  6. &quot;SpeciesB&quot;, &quot;SpeciesC&quot;, &quot;SpeciesD&quot;, &quot;SpeciesE&quot;, &quot;SpeciesF&quot;, &quot;SpeciesG&quot;,
  7. &quot;SpeciesH&quot;, &quot;SpeciesI&quot;, &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;, &quot;SpeciesL&quot;, &quot;SpeciesM&quot;,
  8. &quot;SpeciesA&quot;, &quot;SpeciesB&quot;, &quot;SpeciesC&quot;, &quot;SpeciesD&quot;, &quot;SpeciesE&quot;, &quot;SpeciesF&quot;,
  9. &quot;SpeciesG&quot;, &quot;SpeciesH&quot;, &quot;SpeciesI&quot;, &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;, &quot;SpeciesL&quot;,
  10. &quot;SpeciesM&quot;, &quot;SpeciesA&quot;, &quot;SpeciesB&quot;, &quot;SpeciesC&quot;, &quot;SpeciesD&quot;, &quot;SpeciesE&quot;,
  11. &quot;SpeciesF&quot;, &quot;SpeciesG&quot;, &quot;SpeciesH&quot;, &quot;SpeciesI&quot;, &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;,
  12. &quot;SpeciesL&quot;, &quot;SpeciesM&quot;, &quot;SpeciesA&quot;, &quot;SpeciesB&quot;, &quot;SpeciesC&quot;, &quot;SpeciesD&quot;,
  13. &quot;SpeciesE&quot;, &quot;SpeciesF&quot;, &quot;SpeciesG&quot;, &quot;SpeciesH&quot;, &quot;SpeciesI&quot;, &quot;SpeciesJ&quot;,
  14. &quot;SpeciesK&quot;, &quot;SpeciesL&quot;, &quot;SpeciesM&quot;, &quot;SpeciesA&quot;, &quot;SpeciesB&quot;, &quot;SpeciesC&quot;,
  15. &quot;SpeciesD&quot;, &quot;SpeciesE&quot;, &quot;SpeciesF&quot;, &quot;SpeciesG&quot;, &quot;SpeciesH&quot;, &quot;SpeciesI&quot;,
  16. &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;, &quot;SpeciesL&quot;, &quot;SpeciesM&quot;, &quot;SpeciesA&quot;, &quot;SpeciesB&quot;,
  17. &quot;SpeciesC&quot;, &quot;SpeciesD&quot;, &quot;SpeciesE&quot;, &quot;SpeciesF&quot;, &quot;SpeciesG&quot;, &quot;SpeciesH&quot;,
  18. &quot;SpeciesI&quot;, &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;, &quot;SpeciesL&quot;, &quot;SpeciesM&quot;, &quot;SpeciesA&quot;,
  19. &quot;SpeciesB&quot;, &quot;SpeciesC&quot;, &quot;SpeciesD&quot;, &quot;SpeciesE&quot;, &quot;SpeciesF&quot;, &quot;SpeciesG&quot;,
  20. &quot;SpeciesH&quot;, &quot;SpeciesI&quot;, &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;, &quot;SpeciesL&quot;, &quot;SpeciesM&quot;,
  21. &quot;SpeciesA&quot;, &quot;SpeciesB&quot;, &quot;SpeciesC&quot;, &quot;SpeciesD&quot;, &quot;SpeciesE&quot;, &quot;SpeciesF&quot;,
  22. &quot;SpeciesG&quot;, &quot;SpeciesH&quot;, &quot;SpeciesI&quot;, &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;, &quot;SpeciesL&quot;,
  23. &quot;SpeciesM&quot;, &quot;SpeciesA&quot;, &quot;SpeciesB&quot;, &quot;SpeciesC&quot;, &quot;SpeciesD&quot;, &quot;SpeciesE&quot;,
  24. &quot;SpeciesF&quot;, &quot;SpeciesG&quot;, &quot;SpeciesH&quot;, &quot;SpeciesI&quot;, &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;,
  25. &quot;SpeciesL&quot;, &quot;SpeciesM&quot;, &quot;SpeciesA&quot;, &quot;SpeciesB&quot;, &quot;SpeciesC&quot;, &quot;SpeciesD&quot;,
  26. &quot;SpeciesE&quot;, &quot;SpeciesF&quot;, &quot;SpeciesG&quot;, &quot;SpeciesH&quot;, &quot;SpeciesI&quot;, &quot;SpeciesJ&quot;,
  27. &quot;SpeciesK&quot;, &quot;SpeciesL&quot;, &quot;SpeciesM&quot;, &quot;SpeciesA&quot;, &quot;SpeciesB&quot;, &quot;SpeciesC&quot;,
  28. &quot;SpeciesD&quot;, &quot;SpeciesE&quot;, &quot;SpeciesF&quot;, &quot;SpeciesG&quot;, &quot;SpeciesH&quot;, &quot;SpeciesI&quot;,
  29. &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;, &quot;SpeciesL&quot;, &quot;SpeciesM&quot;), Species2 = structure(c(1L,
  30. 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
  31. 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
  32. 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
  33. 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
  34. 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L,
  35. 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
  36. 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
  37. 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
  38. 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
  39. 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
  40. 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
  41. 13L, 13L, 13L, 13L), .Label = c(&quot;SpeciesA&quot;, &quot;SpeciesB&quot;, &quot;SpeciesC&quot;,
  42. &quot;SpeciesD&quot;, &quot;SpeciesE&quot;, &quot;SpeciesF&quot;, &quot;SpeciesG&quot;, &quot;SpeciesH&quot;, &quot;SpeciesI&quot;,
  43. &quot;SpeciesJ&quot;, &quot;SpeciesK&quot;, &quot;SpeciesL&quot;, &quot;SpeciesM&quot;), class = &quot;factor&quot;),
  44. p_value = c(NA, 8.29661485364936e-14, 0.197328035330918,
  45. 3.73447143215061e-05, 0.0200245227839388, 0.000158518795659732,
  46. 0.999999999999999, 0.999999999999999, 0.200661174254572,
  47. 0.0259711510883781, 0.624415639985824, 0.999999999999995,
  48. 0.999999999999991, 8.29661485364936e-14, NA, 0.911548596172133,
  49. 0.0808672833723648, 0.00232408080140171, 0.00257625010714883,
  50. 0.999999999999777, 0.999999999999777, 0.789829339795786,
  51. 0.198077829941541, 0.768466049890891, 0.999999999999885,
  52. 0.999999999999994, 0.197328035330918, 0.911548596172133,
  53. NA, 0.181696316252709, 0.575238254258972, 0.939866701307512,
  54. 0.999999999999999, 0.999999999999999, 0.999999999999999,
  55. 0.507611967012476, 0.673527222144056, 0.999999999999996,
  56. 1, 3.73447143215061e-05, 0.0808672833723648, 0.181696316252709,
  57. NA, 1.40996305374498e-10, 0.405410680293625, 0.999999999999999,
  58. 0.999999999999999, 1.56797538624063e-08, 8.50447159522988e-05,
  59. 0.00644405295214749, 0.88381428087806, 0.99999999999993,
  60. 0.0200245227839388, 0.00232408080140171, 0.575238254258972,
  61. 1.40996305374498e-10, NA, 0.622095128306733, 0.999999999999959,
  62. 0.999999999999959, 0.000115476294641169, 1.60499061530966e-19,
  63. 4.67520836455185e-05, 0.528580893876124, 0.795044191844885,
  64. 0.000158518795659732, 0.00257625010714883, 0.939866701307512,
  65. 0.405410680293625, 0.622095128306733, NA, 0.999999999999999,
  66. 0.999999999999999, 0.200661174254572, 0.410636112084533,
  67. 0.999999999999929, 0.999999999999995, 0.999999999999991,
  68. 0.999999999999999, 0.999999999999777, 0.999999999999999,
  69. 0.999999999999999, 0.999999999999959, 0.999999999999999,
  70. NA, 0.999999999999299, 0.779426832974571, 0.999999999999933,
  71. 0.999999999999999, 0.999999999999998, 0.999999999999687,
  72. 0.999999999999999, 0.999999999999777, 0.999999999999999,
  73. 0.999999999999999, 0.999999999999959, 0.999999999999999,
  74. 0.999999999999299, NA, 0.999999999999999, 0.611136265859179,
  75. 0.999999999999999, 0.999999999999998, 0.999999999999687,
  76. 0.200661174254572, 0.789829339795786, 0.999999999999999,
  77. 1.56797538624063e-08, 0.000115476294641169, 0.200661174254572,
  78. 0.779426832974571, 0.999999999999999, NA, 0.0311037604732729,
  79. 0.0122054515551129, 0.999999999999984, 0.999999999999999,
  80. 0.0259711510883781, 0.198077829941541, 0.507611967012476,
  81. 8.50447159522988e-05, 1.60499061530966e-19, 0.410636112084533,
  82. 0.999999999999933, 0.611136265859179, 0.0311037604732729,
  83. NA, 0.0403275386741277, 0.508244635418544, 0.999999999999999,
  84. 0.624415639985824, 0.768466049890891, 0.673527222144056,
  85. 0.00644405295214749, 4.67520836455185e-05, 0.999999999999929,
  86. 0.999999999999999, 0.999999999999999, 0.0122054515551129,
  87. 0.0403275386741277, NA, 1, 0.999999999999823, 0.999999999999995,
  88. 0.999999999999885, 0.999999999999996, 0.88381428087806, 0.528580893876124,
  89. 0.999999999999995, 0.999999999999998, 0.999999999999998,
  90. 0.999999999999984, 0.508244635418544, 1, NA, 0.999999999998991,
  91. 0.999999999999991, 0.999999999999994, 1, 0.99999999999993,
  92. 0.795044191844885, 0.999999999999991, 0.999999999999687,
  93. 0.999999999999687, 0.999999999999999, 0.999999999999999,
  94. 0.999999999999823, 0.999999999998991, NA), order_species1 = c(1L,
  95. 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L,
  96. 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L,
  97. 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L,
  98. 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L,
  99. 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L,
  100. 7L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
  101. 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
  102. 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
  103. 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
  104. 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
  105. 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
  106. 13L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L
  107. ), order_species2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
  108. 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
  109. 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
  110. 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L,
  111. 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
  112. 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
  113. 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
  114. 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
  115. 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
  116. 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
  117. 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
  118. 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L,
  119. 13L, 13L, 13L, 13L, 13L, 13L, 13L)), row.names = c(NA, -169L
  120. ), class = &quot;data.frame&quot;)

huangapple
  • 本文由 发表于 2023年2月18日 11:35:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491011.html
匿名

发表评论

匿名网友

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

确定