rggplot2tibbleggpubrviolin-plot

Object missing or new axis labels made using ggpubr::stat_pvalue_manual on violin plot with fill aspect


I have ran into a problem with using ggpubr::stat_pvalue_manual with a ggplot2 violin plot that has a fill aspect. There is either an issue of the fill aspect group not being found as an object or the brackets are misaligned completely. To start off I have easily made a violin/boxplot plot with the pairwise brackets using ggplot, but before that I make the table of adjusted p values from the contrasts of my estimated marginal means. More information about estimated marginal means can be found in emmeans package. Below is an example of what I would like the plot to look like without the fill aspect.

    ## make table of adjusted p values
stat.test <- tibble::tribble(
        ~group1,               ~group2,               ~p.adj,
        "Non-brooding",        "Brooding_wo_embryos", 1.0000,
        "Non-brooding",        "Brooding_w_embryos",  0.0001,            
        "Brooding_wo_embryos", "Brooding_w_embryos",  0.0001)

## test figure for stackoverflow
library(ggplot2)
ggplot(x, aes(x=Reproductive, y=Total_Phosphorus)) + 
  geom_violin(width = 1, size = 1) +
  geom_boxplot(position = position_dodge(width = 1), width = 0.1, color="black", alpha=0.5, size = 1) +
## pairwise comparisons added here
  stat_pvalue_manual(stat.test, y.position = 0.00125, step.increase = 0.2, label = "p.adj")

I get the expected outcome: test violin/boxplot

Now I would like to do the same with a plot that has a fill aspect but R keeps telling me that I P_treatment is not an object, but I believe that P_treatment is an object since dataframe$P_treatment returns the item.

First attempted adjusted p value table does not work:

stat.test2 <- tibble::tribble(
  ~group1,                            ~group2,                            ~p.adj,
  "Intermediate Non-brooding",        "Low Non-brooding",                 0.6991,
  "Intermediate Non-brooding",        "Intermediate Brooding_wo_embryos", 1.0000,            
  "Intermediate Non-brooding",        "Low Brooding_wo_embryos",          1.0000,
  "Intermediate Non-brooding",        "Intermediate Brooding_w_embryos",  0.0001, 
  "Intermediate Non-brooding",        "Low Brooding_w_embryos",           0.0001,
  "Low Non-brooding",                 "Intermediate Brooding_wo_embryos", 1.0000, 
  "Low Non-brooding",                 "Low Brooding_wo_embryos",          1.0000,
  "Low Non-brooding",                 "Intermediate Brooding_w_embryos",  0.0001,
  "Low Non-brooding",                 "Low Brooding_w_embryos",           0.0001, 
  "Intermediate Brooding_wo_embryos", "Low Brooding_wo_embryos",          1.0000, 
  "Intermediate Brooding_wo_embryos", "Intermediate Brooding_w_embryos",  0.0001,
  "Intermediate Brooding_wo_embryos", "Low Brooding_w_embryos",           0.0001, 
  "Low Brooding_wo_embryos",          "Intermediate Brooding_w_embryos",  0.0001,
  "Low Brooding_wo_embryos",          "Low Brooding_w_embryos",           0.0001,
 "Intermediate Brooding_w_embryos",   "Low Brooding_w_embryos",           1.0000)

## test figure for stack overflow
library(ggplot2)
ggplot(x, aes(x=Reproductive, y=Total_Phosphorus, shape = P_treatment)) + 
  geom_violin(width = 1) +
  geom_boxplot(position = position_dodge(width = 1), width = 0.1, color="black", alpha=0.5) +
  stat_pvalue_manual(stat.test2, y.position = 0.00125, step.increase = 0.1, label = "p.adj")

Error: ! Problem while computing aesthetics. ℹ Error occurred in the 3rd layer. Caused by error: ! object 'P_treatment' not found Run rlang::last_trace() to see where the error occurred.

I tried this method again, but I define P_treatment. I know I did not define P_treatment in the table of p values correctly, but this is just a test. Now, the issue is my brackets are attached to vectors that aren't real and all the way to the right.

Second attempted adjusted p value table does not work:

stat.test2 <- tibble::tribble(
  ~P_treatment,  ~group1,                            ~group2,                            ~p.adj,
"Intermediate", "Intermediate Non-brooding",        "Low Non-brooding",                 0.6991,
"Intermediate", "Intermediate Non-brooding",        "Intermediate Brooding_wo_embryos", 1.0000,         
"Intermediate", "Intermediate Non-brooding",        "Low Brooding_wo_embryos",          1.0000,
"Intermediate", "Intermediate Non-brooding",        "Intermediate Brooding_w_embryos",  0.0001, 
"Intermediate", "Intermediate Non-brooding",        "Low Brooding_w_embryos",           0.0001,
"Intermediate", "Low Non-brooding",                 "Intermediate Brooding_wo_embryos", 1.0000, 
"Intermediate", "Low Non-brooding",                 "Low Brooding_wo_embryos",          1.0000,
"Intermediate", "Low Non-brooding",                 "Intermediate Brooding_w_embryos",  0.0001,
"Intermediate", "Low Non-brooding",                 "Low Brooding_w_embryos",           0.0001, 
"Intermediate", "Intermediate Brooding_wo_embryos", "Low Brooding_wo_embryos",          1.0000, 
"Intermediate", "Intermediate Brooding_wo_embryos", "Intermediate Brooding_w_embryos",  0.0001,
"Intermediate", "Intermediate Brooding_wo_embryos", "Low Brooding_w_embryos",           0.0001, 
"Intermediate", "Low Brooding_wo_embryos",          "Intermediate Brooding_w_embryos",  0.0001,
"Intermediate", "Low Brooding_wo_embryos",          "Low Brooding_w_embryos",           0.0001,
"Intermediate", "Intermediate Brooding_w_embryos",   "Low Brooding_w_embryos",           1.0000)

library(ggplot2)
ggplot(x, aes(x=Reproductive, y=Total_Phosphorus, shape = P_treatment)) + 
  geom_violin(width = 1) +
  geom_boxplot(position = position_dodge(width = 1), width = 0.1, color="black", alpha=0.5) +
  stat_pvalue_manual(stat.test2, y.position = 0.00125, step.increase = 0.1, label = "p.adj")

test figure

Third attempted adjusted p value table does not work:

stat.test2 <- tibble::tribble(
~Reproductive,   ~P_treatment,  ~group1,                            ~group2,                            ~p.adj,
"Non-brooding", "Intermediate", "Intermediate Non-brooding",        "Low Non-brooding",                 0.6991,
"Non-brooding", "Intermediate", "Intermediate Non-brooding",        "Intermediate Brooding_wo_embryos", 1.0000, 
"Non-brooding", "Intermediate", "Intermediate Non-brooding",        "Low Brooding_wo_embryos",          1.0000,
"Non-brooding", "Intermediate", "Intermediate Non-brooding",        "Intermediate Brooding_w_embryos",  0.0001, 
"Non-brooding", "Intermediate", "Intermediate Non-brooding",        "Low Brooding_w_embryos",           0.0001,
"Non-brooding", "Intermediate", "Low Non-brooding",                 "Intermediate Brooding_wo_embryos", 1.0000, 
"Non-brooding", "Intermediate", "Low Non-brooding",                 "Low Brooding_wo_embryos",          1.0000,
"Non-brooding", "Intermediate", "Low Non-brooding",                 "Intermediate Brooding_w_embryos",  0.0001,
"Non-brooding", "Intermediate", "Low Non-brooding",                 "Low Brooding_w_embryos",           0.0001, 
"Non-brooding", "Intermediate", "Intermediate Brooding_wo_embryos", "Low Brooding_wo_embryos",          1.0000, 
"Non-brooding", "Intermediate", "Intermediate Brooding_wo_embryos", "Intermediate Brooding_w_embryos",  0.0001,
"Non-brooding", "Intermediate", "Intermediate Brooding_wo_embryos", "Low Brooding_w_embryos",           0.0001, 
"Non-brooding", "Intermediate", "Low Brooding_wo_embryos",          "Intermediate Brooding_w_embryos",  0.0001,
"Non-brooding", "Intermediate", "Low Brooding_wo_embryos",          "Low Brooding_w_embryos",           0.0001,
"Non-brooding", "Intermediate", "Intermediate Brooding_w_embryos",   "Low Brooding_w_embryos",           1.0000)

## test figure for stack overflow
library(ggplot2)
ggplot(x, aes(x=Reproductive, y=Total_Phosphorus, shape = P_treatment)) + 
  geom_violin(width = 1) +
  geom_boxplot(position = position_dodge(width = 1), width = 0.1, color="black", alpha=0.5) +
  stat_pvalue_manual(stat.test2, y.position = 0.00125, step.increase = 0.1, label = "p.adj")

test figure

Here is when I noticed that my table makes new groups out of my data. However, my data is not structured as a mix P_treatment and Reproductive - they are separate columns. You might have to click on this image as it is very blurry! X axis zoomed in

My data:

A tibble: 209 × 3

    P_treatment  Reproductive        Total_Phosphorus
    <fct>        <fct>                          <dbl>
  1 Low          Non-brooding               0.000321 
  2 Intermediate Brooding_w_embryos         0.000733 
  3 Intermediate Brooding_wo_embryos        0.000341 
  4 Low          Brooding_w_embryos         0.00101  
  5 Low          Brooding_wo_embryos        0.000410 
  6 Intermediate Non-brooding               0.000497 
  7 Intermediate Brooding_w_embryos         0.000697 
  8 Intermediate Brooding_wo_embryos        0.000415 
  9 Low          Brooding_w_embryos         0.000491 
 10 Low          Brooding_w_embryos         0.000863 
 11 Low          Brooding_wo_embryos        0.000273 
 12 Low          Brooding_wo_embryos        0.000365 
 13 Low          Non-brooding               0.000556 
 14 Low          Non-brooding               0.000317 
 15 Intermediate Brooding_w_embryos         0.000804 
 16 Intermediate Brooding_wo_embryos        0.000498 
 17 Low          Brooding_w_embryos         0.000708 
 18 Low          Brooding_wo_embryos        0.000402 
 19 Low          Brooding_wo_embryos        0.000398 
 20 Low          Brooding_w_embryos         0.000628 
 21 Low          Brooding_wo_embryos        0.000315 
 22 Low          Non-brooding               0.000619 
 23 Intermediate Brooding_w_embryos         0.000674 
 24 Intermediate Brooding_wo_embryos        0.000276 
 25 Intermediate Brooding_w_embryos         0.000899 
 26 Intermediate Brooding_wo_embryos        0.000420 
 27 Intermediate Non-brooding               0.000468 
 28 Low          Brooding_w_embryos         0.000715 
 29 Low          Brooding_w_embryos         0.000842 
 30 Low          Brooding_wo_embryos        0.000443 
 31 Low          Brooding_wo_embryos        0.000517 
 32 Low          Brooding_w_embryos         0.000843 
 33 Low          Brooding_wo_embryos        0.000589 
 34 Low          Brooding_w_embryos         0.00100  
 35 Low          Brooding_wo_embryos        0.000470 
 36 Intermediate Brooding_w_embryos         0.000573 
 37 Intermediate Brooding_w_embryos         0.000785 
 38 Intermediate Brooding_wo_embryos        0.000162 
 39 Intermediate Brooding_wo_embryos        0.000507 
 40 Low          Non-brooding               0.000249 
 41 Intermediate Brooding_w_embryos         0.00078  
 42 Intermediate Brooding_wo_embryos        0.000450 
 43 Low          Non-brooding               0.000327 
 44 Low          Non-brooding               0.000322 
 45 Low          Brooding_w_embryos         0.00091  
 46 Low          Brooding_wo_embryos        0.000480 
 47 Intermediate Brooding_w_embryos         0.000954 
 48 Intermediate Brooding_wo_embryos        0.000472 
 49 Intermediate Non-brooding               0.000461 
 50 Low          Brooding_w_embryos         0.00024  
 51 Low          Brooding_w_embryos         0.000749 
 52 Low          Brooding_w_embryos         0.000713 
 53 Low          Brooding_wo_embryos        0.000122 
 54 Low          Brooding_wo_embryos        0.000336 
 55 Low          Brooding_wo_embryos        0.000419 
 56 Low          Non-brooding               0.000319 
 57 Low          Non-brooding               0.000301 
 58 Intermediate Non-brooding               0.000341 
 59 Low          Non-brooding               0.000733 
 60 Intermediate Brooding_w_embryos         0.000688 
 61 Intermediate Brooding_w_embryos         0.000828 
 62 Intermediate Brooding_wo_embryos        0.0000546
 63 Intermediate Brooding_wo_embryos        0.000341 
 64 Intermediate Non-brooding               0.000501 
 65 Intermediate Non-brooding               0.000367 
 66 Low          Brooding_w_embryos         0.00078  
 67 Low          Brooding_wo_embryos        0.000522 
 68 Low          Non-brooding               0.000560 
 69 Intermediate Brooding_wo_embryos        0.000329 
 70 Intermediate Non-brooding               0.000340 
 71 Intermediate Non-brooding               0.000438 
 72 Intermediate Non-brooding               0.000304 
 73 Low          Non-brooding               0.000291 
 74 Low          Brooding_wo_embryos        0.000507 
 75 Low          Non-brooding               0.000462 
 76 Intermediate Non-brooding               0.000127 
 77 Intermediate Brooding_w_embryos         0.000804 
 78 Intermediate Brooding_w_embryos         0.000446 
 79 Intermediate Brooding_w_embryos         0.000747 
 80 Intermediate Brooding_wo_embryos        0.000297 
 81 Intermediate Brooding_wo_embryos        0.000308 
 82 Intermediate Brooding_wo_embryos        0.000348 
 83 Low          Non-brooding               0.000500 
 84 Low          Brooding_wo_embryos        0.000512 
 85 Intermediate Brooding_w_embryos         0.000822 
 86 Intermediate Brooding_w_embryos         0.00108  
 87 Intermediate Brooding_wo_embryos        0.000436 
 88 Intermediate Brooding_wo_embryos        0.000560 
 89 Intermediate Brooding_w_embryos         0.000871 
 90 Intermediate Brooding_wo_embryos        0.000265 
 91 Low          Brooding_w_embryos         0.000485 
 92 Low          Brooding_wo_embryos        0.000241 
 93 Low          Non-brooding               0.000608 
 94 Low          Non-brooding               0.000233 
 95 Low          Non-brooding               0.000481 
 96 Low          Brooding_w_embryos         0.00106  
 97 Low          Brooding_wo_embryos        0.000596 
 98 Intermediate Non-brooding               0.000254 
 99 Low          Non-brooding               0.000596 
100 Low          Non-brooding               0.000481 
101 Low          Brooding_w_embryos         0.000588 
102 Low          Brooding_w_embryos         0.000574 
103 Low          Brooding_w_embryos         0.00132  
104 Low          Brooding_wo_embryos        0.0000973
105 Low          Brooding_wo_embryos        0.000281 
106 Low          Brooding_wo_embryos        0.000291 
107 Low          Non-brooding               0.000265 
108 Low          Non-brooding               0.000158 
109 Intermediate Brooding_w_embryos         0.00107  
110 Intermediate Brooding_w_embryos         0.000415 
111 Intermediate Brooding_w_embryos         0.000686 
112 Intermediate Brooding_w_embryos         0.000583 
113 Intermediate Brooding_w_embryos         0.000852 
114 Intermediate Brooding_wo_embryos        0.000565 
115 Intermediate Brooding_wo_embryos        0.000137 
116 Intermediate Brooding_wo_embryos        0.000314 
117 Intermediate Brooding_wo_embryos        0.000295 
118 Intermediate Brooding_wo_embryos        0.000355 
119 Intermediate Non-brooding               0.000347 
120 Intermediate Non-brooding               0.000676 
121 Intermediate Non-brooding               0.000347 
122 Low          Brooding_w_embryos         0.000682 
123 Low          Brooding_wo_embryos        0.000298 
124 Low          Non-brooding               0.000639 
125 Low          Non-brooding               0.000368 
126 Intermediate Brooding_w_embryos         0.000974 
127 Intermediate Brooding_wo_embryos        0.000460 
128 Intermediate Non-brooding               0.000340 
129 Intermediate Brooding_w_embryos         0.0006   
130 Intermediate Brooding_w_embryos         0.000706 
131 Intermediate Brooding_wo_embryos        0.000351 
132 Intermediate Brooding_wo_embryos        0.000300 
133 Intermediate Non-brooding               0.000382 
134 Low          Brooding_wo_embryos        0.000422 
135 Low          Non-brooding               0.000405 
136 Low          Non-brooding               0.000300 
137 Low          Brooding_w_embryos         0.000839 
138 Low          Brooding_wo_embryos        0.000580 
139 Intermediate Brooding_w_embryos         0.0007   
140 Intermediate Brooding_wo_embryos        0.000241 
141 Intermediate Non-brooding               0.000413 
142 Intermediate Non-brooding               0.0000462
143 Low          Brooding_w_embryos         0.00106  
144 Low          Brooding_wo_embryos        0.000408 
145 Low          Non-brooding               0.000239 
146 Low          Non-brooding               0.000416 
147 Low          Non-brooding               0.000330 
148 Low          Non-brooding               0.000365 
149 Intermediate Non-brooding               0.000317 
150 Low          Non-brooding               0.000414 
151 Low          Non-brooding               0.000434 
152 Intermediate Non-brooding               0.0000394
153 Intermediate Brooding_w_embryos         0.000722 
154 Intermediate Brooding_wo_embryos        0.000299 
155 Low          Brooding_w_embryos         0.00104  
156 Low          Brooding_wo_embryos        0.000404 
157 Low          Non-brooding               0.000238 
158 Intermediate Brooding_w_embryos         0.000757 
159 Intermediate Brooding_w_embryos         0.000991 
160 Intermediate Brooding_wo_embryos        0.000304 
161 Intermediate Brooding_wo_embryos        0.000332 
162 Intermediate Non-brooding               0.000239 
163 Intermediate Non-brooding               0.000494 
164 Intermediate Non-brooding               0.000269 
165 Low          Brooding_w_embryos         0.000839 
166 Low          Brooding_wo_embryos        0.000392 
167 Low          Non-brooding               0.000433 
168 Low          Non-brooding               0.000506 
169 Intermediate Non-brooding               0.000449 
170 Intermediate Non-brooding               0.000180 
171 Low          Non-brooding               0.000406 
172 Intermediate Brooding_w_embryos         0.000944 
173 Intermediate Brooding_w_embryos         0.000602 
174 Intermediate Brooding_wo_embryos        0.000452 
175 Intermediate Brooding_wo_embryos        0.000331 
176 Intermediate Non-brooding               0.000260 
177 Low          Non-brooding               0.000299 
178 Intermediate Brooding_w_embryos         0.000797 
179 Intermediate Brooding_w_embryos         0.000619 
180 Intermediate Brooding_wo_embryos        0.000391 
181 Intermediate Brooding_wo_embryos        0.000358 
182 Low          Non-brooding               0.000640 
183 Intermediate Brooding_w_embryos         0.000799 
184 Intermediate Brooding_wo_embryos        0.000340 
185 Low          Brooding_w_embryos         0.000779 
186 Low          Brooding_w_embryos         0.000769 
187 Low          Brooding_wo_embryos        0.000304 
188 Low          Brooding_wo_embryos        0.000450 
189 Intermediate Brooding_w_embryos         0.000609 
190 Intermediate Brooding_wo_embryos        0.000376 
191 Intermediate Non-brooding               0.000313 
192 Low          Brooding_wo_embryos        0.000456 
193 Intermediate Brooding_w_embryos         0.000751 
194 Intermediate Brooding_w_embryos         0.000453 
195 Intermediate Brooding_wo_embryos        0.000378 
196 Intermediate Brooding_wo_embryos        0.000200 
197 Intermediate Brooding_w_embryos         0.000563 
198 Intermediate Brooding_wo_embryos        0.000342 
199 Low          Non-brooding               0.000485 
200 Intermediate Brooding_w_embryos         0.00109  
201 Intermediate Brooding_wo_embryos        0.000567 
202 Low          Brooding_w_embryos         0.00096  
203 Low          Brooding_wo_embryos        0.000598 
204 Intermediate Brooding_w_embryos         0.00063  
205 Intermediate Brooding_wo_embryos        0.000408 
206 Low          Brooding_w_embryos         0.00133  
207 Low          Brooding_wo_embryos        0.000517 
208 Intermediate Brooding_w_embryos         0.00108  
209 Intermediate Brooding_wo_embryos        0.000557 

I would like to note here that dataframe$P_treatment has 2 levels and dataframe$Reproductive has 3 levels. I know that all groups are not in one P_treatment or Reproductive mode level, but I tried using one variation of the group as a test and there still was no difference between second and third attempt.

This method has worked before with no fill so I am confused as to it will not work now. Thank you very much!


Solution

  • You have factor levels in your dataframe "x" (e.g. "Non-brooding") that aren't in "stat.test2" ("Low Non-brooding"). If you create the equivalent factor levels (using e.g. interaction() ) ggplot2 doesn't return the error, e.g.

    Load libraries and data:

    library(tidyverse)
    library(ggpubr)
    
    df <- read.table(text = "rn  P_treatment  Reproductive        Total_Phosphorus
      1 Low          Non-brooding               0.000321 
      2 Intermediate Brooding_w_embryos         0.000733 
      3 Intermediate Brooding_wo_embryos        0.000341 
      4 Low          Brooding_w_embryos         0.00101  
      5 Low          Brooding_wo_embryos        0.000410 
      6 Intermediate Non-brooding               0.000497 
      7 Intermediate Brooding_w_embryos         0.000697 
      8 Intermediate Brooding_wo_embryos        0.000415 
      9 Low          Brooding_w_embryos         0.000491 
     10 Low          Brooding_w_embryos         0.000863 
     11 Low          Brooding_wo_embryos        0.000273 
     12 Low          Brooding_wo_embryos        0.000365 
     13 Low          Non-brooding               0.000556 
     14 Low          Non-brooding               0.000317 
     15 Intermediate Brooding_w_embryos         0.000804 
     16 Intermediate Brooding_wo_embryos        0.000498 
     17 Low          Brooding_w_embryos         0.000708 
     18 Low          Brooding_wo_embryos        0.000402 
     19 Low          Brooding_wo_embryos        0.000398 
     20 Low          Brooding_w_embryos         0.000628 
     21 Low          Brooding_wo_embryos        0.000315 
     22 Low          Non-brooding               0.000619 
     23 Intermediate Brooding_w_embryos         0.000674 
     24 Intermediate Brooding_wo_embryos        0.000276 
     25 Intermediate Brooding_w_embryos         0.000899 
     26 Intermediate Brooding_wo_embryos        0.000420 
     27 Intermediate Non-brooding               0.000468 
     28 Low          Brooding_w_embryos         0.000715 
     29 Low          Brooding_w_embryos         0.000842 
     30 Low          Brooding_wo_embryos        0.000443 
     31 Low          Brooding_wo_embryos        0.000517 
     32 Low          Brooding_w_embryos         0.000843 
     33 Low          Brooding_wo_embryos        0.000589 
     34 Low          Brooding_w_embryos         0.00100  
     35 Low          Brooding_wo_embryos        0.000470 
     36 Intermediate Brooding_w_embryos         0.000573 
     37 Intermediate Brooding_w_embryos         0.000785 
     38 Intermediate Brooding_wo_embryos        0.000162 
     39 Intermediate Brooding_wo_embryos        0.000507 
     40 Low          Non-brooding               0.000249 
     41 Intermediate Brooding_w_embryos         0.00078  
     42 Intermediate Brooding_wo_embryos        0.000450 
     43 Low          Non-brooding               0.000327 
     44 Low          Non-brooding               0.000322 
     45 Low          Brooding_w_embryos         0.00091  
     46 Low          Brooding_wo_embryos        0.000480 
     47 Intermediate Brooding_w_embryos         0.000954 
     48 Intermediate Brooding_wo_embryos        0.000472 
     49 Intermediate Non-brooding               0.000461 
     50 Low          Brooding_w_embryos         0.00024  
     51 Low          Brooding_w_embryos         0.000749 
     52 Low          Brooding_w_embryos         0.000713 
     53 Low          Brooding_wo_embryos        0.000122 
     54 Low          Brooding_wo_embryos        0.000336 
     55 Low          Brooding_wo_embryos        0.000419 
     56 Low          Non-brooding               0.000319 
     57 Low          Non-brooding               0.000301 
     58 Intermediate Non-brooding               0.000341 
     59 Low          Non-brooding               0.000733 
     60 Intermediate Brooding_w_embryos         0.000688 
     61 Intermediate Brooding_w_embryos         0.000828 
     62 Intermediate Brooding_wo_embryos        0.0000546
     63 Intermediate Brooding_wo_embryos        0.000341 
     64 Intermediate Non-brooding               0.000501 
     65 Intermediate Non-brooding               0.000367 
     66 Low          Brooding_w_embryos         0.00078  
     67 Low          Brooding_wo_embryos        0.000522 
     68 Low          Non-brooding               0.000560 
     69 Intermediate Brooding_wo_embryos        0.000329 
     70 Intermediate Non-brooding               0.000340 
     71 Intermediate Non-brooding               0.000438 
     72 Intermediate Non-brooding               0.000304 
     73 Low          Non-brooding               0.000291 
     74 Low          Brooding_wo_embryos        0.000507 
     75 Low          Non-brooding               0.000462 
     76 Intermediate Non-brooding               0.000127 
     77 Intermediate Brooding_w_embryos         0.000804 
     78 Intermediate Brooding_w_embryos         0.000446 
     79 Intermediate Brooding_w_embryos         0.000747 
     80 Intermediate Brooding_wo_embryos        0.000297 
     81 Intermediate Brooding_wo_embryos        0.000308 
     82 Intermediate Brooding_wo_embryos        0.000348 
     83 Low          Non-brooding               0.000500 
     84 Low          Brooding_wo_embryos        0.000512 
     85 Intermediate Brooding_w_embryos         0.000822 
     86 Intermediate Brooding_w_embryos         0.00108  
     87 Intermediate Brooding_wo_embryos        0.000436 
     88 Intermediate Brooding_wo_embryos        0.000560 
     89 Intermediate Brooding_w_embryos         0.000871 
     90 Intermediate Brooding_wo_embryos        0.000265 
     91 Low          Brooding_w_embryos         0.000485 
     92 Low          Brooding_wo_embryos        0.000241 
     93 Low          Non-brooding               0.000608 
     94 Low          Non-brooding               0.000233 
     95 Low          Non-brooding               0.000481 
     96 Low          Brooding_w_embryos         0.00106  
     97 Low          Brooding_wo_embryos        0.000596 
     98 Intermediate Non-brooding               0.000254 
     99 Low          Non-brooding               0.000596 
    100 Low          Non-brooding               0.000481 
    101 Low          Brooding_w_embryos         0.000588 
    102 Low          Brooding_w_embryos         0.000574 
    103 Low          Brooding_w_embryos         0.00132  
    104 Low          Brooding_wo_embryos        0.0000973
    105 Low          Brooding_wo_embryos        0.000281 
    106 Low          Brooding_wo_embryos        0.000291 
    107 Low          Non-brooding               0.000265 
    108 Low          Non-brooding               0.000158 
    109 Intermediate Brooding_w_embryos         0.00107  
    110 Intermediate Brooding_w_embryos         0.000415 
    111 Intermediate Brooding_w_embryos         0.000686 
    112 Intermediate Brooding_w_embryos         0.000583 
    113 Intermediate Brooding_w_embryos         0.000852 
    114 Intermediate Brooding_wo_embryos        0.000565 
    115 Intermediate Brooding_wo_embryos        0.000137 
    116 Intermediate Brooding_wo_embryos        0.000314 
    117 Intermediate Brooding_wo_embryos        0.000295 
    118 Intermediate Brooding_wo_embryos        0.000355 
    119 Intermediate Non-brooding               0.000347 
    120 Intermediate Non-brooding               0.000676 
    121 Intermediate Non-brooding               0.000347 
    122 Low          Brooding_w_embryos         0.000682 
    123 Low          Brooding_wo_embryos        0.000298 
    124 Low          Non-brooding               0.000639 
    125 Low          Non-brooding               0.000368 
    126 Intermediate Brooding_w_embryos         0.000974 
    127 Intermediate Brooding_wo_embryos        0.000460 
    128 Intermediate Non-brooding               0.000340 
    129 Intermediate Brooding_w_embryos         0.0006   
    130 Intermediate Brooding_w_embryos         0.000706 
    131 Intermediate Brooding_wo_embryos        0.000351 
    132 Intermediate Brooding_wo_embryos        0.000300 
    133 Intermediate Non-brooding               0.000382 
    134 Low          Brooding_wo_embryos        0.000422 
    135 Low          Non-brooding               0.000405 
    136 Low          Non-brooding               0.000300 
    137 Low          Brooding_w_embryos         0.000839 
    138 Low          Brooding_wo_embryos        0.000580 
    139 Intermediate Brooding_w_embryos         0.0007   
    140 Intermediate Brooding_wo_embryos        0.000241 
    141 Intermediate Non-brooding               0.000413 
    142 Intermediate Non-brooding               0.0000462
    143 Low          Brooding_w_embryos         0.00106  
    144 Low          Brooding_wo_embryos        0.000408 
    145 Low          Non-brooding               0.000239 
    146 Low          Non-brooding               0.000416 
    147 Low          Non-brooding               0.000330 
    148 Low          Non-brooding               0.000365 
    149 Intermediate Non-brooding               0.000317 
    150 Low          Non-brooding               0.000414 
    151 Low          Non-brooding               0.000434 
    152 Intermediate Non-brooding               0.0000394
    153 Intermediate Brooding_w_embryos         0.000722 
    154 Intermediate Brooding_wo_embryos        0.000299 
    155 Low          Brooding_w_embryos         0.00104  
    156 Low          Brooding_wo_embryos        0.000404 
    157 Low          Non-brooding               0.000238 
    158 Intermediate Brooding_w_embryos         0.000757 
    159 Intermediate Brooding_w_embryos         0.000991 
    160 Intermediate Brooding_wo_embryos        0.000304 
    161 Intermediate Brooding_wo_embryos        0.000332 
    162 Intermediate Non-brooding               0.000239 
    163 Intermediate Non-brooding               0.000494 
    164 Intermediate Non-brooding               0.000269 
    165 Low          Brooding_w_embryos         0.000839 
    166 Low          Brooding_wo_embryos        0.000392 
    167 Low          Non-brooding               0.000433 
    168 Low          Non-brooding               0.000506 
    169 Intermediate Non-brooding               0.000449 
    170 Intermediate Non-brooding               0.000180 
    171 Low          Non-brooding               0.000406 
    172 Intermediate Brooding_w_embryos         0.000944 
    173 Intermediate Brooding_w_embryos         0.000602 
    174 Intermediate Brooding_wo_embryos        0.000452 
    175 Intermediate Brooding_wo_embryos        0.000331 
    176 Intermediate Non-brooding               0.000260 
    177 Low          Non-brooding               0.000299 
    178 Intermediate Brooding_w_embryos         0.000797 
    179 Intermediate Brooding_w_embryos         0.000619 
    180 Intermediate Brooding_wo_embryos        0.000391 
    181 Intermediate Brooding_wo_embryos        0.000358 
    182 Low          Non-brooding               0.000640 
    183 Intermediate Brooding_w_embryos         0.000799 
    184 Intermediate Brooding_wo_embryos        0.000340 
    185 Low          Brooding_w_embryos         0.000779 
    186 Low          Brooding_w_embryos         0.000769 
    187 Low          Brooding_wo_embryos        0.000304 
    188 Low          Brooding_wo_embryos        0.000450 
    189 Intermediate Brooding_w_embryos         0.000609 
    190 Intermediate Brooding_wo_embryos        0.000376 
    191 Intermediate Non-brooding               0.000313 
    192 Low          Brooding_wo_embryos        0.000456 
    193 Intermediate Brooding_w_embryos         0.000751 
    194 Intermediate Brooding_w_embryos         0.000453 
    195 Intermediate Brooding_wo_embryos        0.000378 
    196 Intermediate Brooding_wo_embryos        0.000200 
    197 Intermediate Brooding_w_embryos         0.000563 
    198 Intermediate Brooding_wo_embryos        0.000342 
    199 Low          Non-brooding               0.000485 
    200 Intermediate Brooding_w_embryos         0.00109  
    201 Intermediate Brooding_wo_embryos        0.000567 
    202 Low          Brooding_w_embryos         0.00096  
    203 Low          Brooding_wo_embryos        0.000598 
    204 Intermediate Brooding_w_embryos         0.00063  
    205 Intermediate Brooding_wo_embryos        0.000408 
    206 Low          Brooding_w_embryos         0.00133  
    207 Low          Brooding_wo_embryos        0.000517 
    208 Intermediate Brooding_w_embryos         0.00108  
    209 Intermediate Brooding_wo_embryos        0.000557", header = TRUE)
    
    df$rn <- NULL
    
    stat.test2 <- tibble::tribble(
      ~group1,                            ~group2,                            ~p.adj,
      "Intermediate Non-brooding",        "Low Non-brooding",                 0.6991,
      "Intermediate Non-brooding",        "Intermediate Brooding_wo_embryos", 1.0000,            
      "Intermediate Non-brooding",        "Low Brooding_wo_embryos",          1.0000,
      "Intermediate Non-brooding",        "Intermediate Brooding_w_embryos",  0.0001, 
      "Intermediate Non-brooding",        "Low Brooding_w_embryos",           0.0001,
      "Low Non-brooding",                 "Intermediate Brooding_wo_embryos", 1.0000, 
      "Low Non-brooding",                 "Low Brooding_wo_embryos",          1.0000,
      "Low Non-brooding",                 "Intermediate Brooding_w_embryos",  0.0001,
      "Low Non-brooding",                 "Low Brooding_w_embryos",           0.0001, 
      "Intermediate Brooding_wo_embryos", "Low Brooding_wo_embryos",          1.0000, 
      "Intermediate Brooding_wo_embryos", "Intermediate Brooding_w_embryos",  0.0001,
      "Intermediate Brooding_wo_embryos", "Low Brooding_w_embryos",           0.0001, 
      "Low Brooding_wo_embryos",          "Intermediate Brooding_w_embryos",  0.0001,
      "Low Brooding_wo_embryos",          "Low Brooding_w_embryos",           0.0001,
      "Intermediate Brooding_w_embryos",   "Low Brooding_w_embryos",           1.0000)
    

    Figure:

    ggplot(df, aes(x=interaction(P_treatment, Reproductive, sep = " "),
                   y=Total_Phosphorus)) + 
      geom_violin(aes(fill = P_treatment),
                  width = 1) +
      geom_boxplot(position = position_dodge(width = 1), width = 0.1, color="black", alpha=0.5) +
      stat_pvalue_manual(stat.test2, y.position = 0.00125, step.increase = 0.1, label = "p.adj") +
      theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))
    

    violin plot

    Created on 2025-03-20 with reprex v2.1.1

    Does that work for your use-case?