<p>The problem is the scale used: For the plot you called "weird" (first from the top), the scale is 50 and for the "ggplot only" (third from the top) the scale is 1.</p>

<p>You should play with the <code>stat_compare_means(label.y = 50)</code> bit, you can try setting the <code>label.y</code> parameter to 1.5 or 2.</p>

<p>EDIT BECAUSE OF THE ADDITIONAL REMARKS ON THE QUESTION:</p>

<p>Once again (and for sure the last time), I had to manually create example data as OP ignores repeated reminders on <a href="https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example">how to make a reproducible example with R</a>.</p>

<pre><code>library(data.table)
library(ggpubr)

text &lt;- "
treatment variable     value
1       pre     Sig1 0.1173863
2       pre     Sig1 0.1888243
3       pre     Sig1 0.2191765
4       pre     Sig1 0.1277735
5       pre     Sig1 0.1711712
6       pre     Sig1 0.0000000
1       post     Sig1 0.1173863
2       post     Sig1 0.1888243
3       post     Sig1 0.2191765
4       post     Sig1 0.1277735
5       post     Sig1 0.1711712
6       post     Sig1 0.0000000
1021      pre     Sig9     0
1022      pre     Sig9     0
1023      pre     Sig9     0
1024      pre     Sig9     0
1025      pre     Sig9     0
1026      pre     Sig9     0
1021      post     Sig9     1
1022      post     Sig9     1
1023      post     Sig9     1
1024      post     Sig9     1
1025      post     Sig9     1
1026      post     Sig9     1
"
my_data &lt;-fread(text)

&gt; my_data
      V1 treatment variable     value
 1:    1       pre     Sig1 0.1173863
 2:    2       pre     Sig1 0.1888243
 3:    3       pre     Sig1 0.2191765
 4:    4       pre     Sig1 0.1277735
 5:    5       pre     Sig1 0.1711712
 6:    6       pre     Sig1 0.0000000
 7:    1      post     Sig1 0.1173863
 8:    2      post     Sig1 0.1888243
 9:    3      post     Sig1 0.2191765
10:    4      post     Sig1 0.1277735
11:    5      post     Sig1 0.1711712
12:    6      post     Sig1 0.0000000
13: 1021       pre     Sig9 0.0000000
14: 1022       pre     Sig9 0.0000000
15: 1023       pre     Sig9 0.0000000
16: 1024       pre     Sig9 0.0000000
17: 1025       pre     Sig9 0.0000000
18: 1026       pre     Sig9 0.0000000
19: 1021      post     Sig9 1.0000000
20: 1022      post     Sig9 1.0000000
21: 1023      post     Sig9 1.0000000
22: 1024      post     Sig9 1.0000000
23: 1025      post     Sig9 1.0000000
24: 1026      post     Sig9 1.0000000
      V1 treatment variable     value

ggboxplot(my_data,
          x = "treatment",
          y = "value",
          color = "treatment",
          facet.by = "variable") + 
  stat_compare_means(label = "p.format")
</code></pre>

<p>And this code generates:</p>

<p><a href="https://i.sstatic.net/3n2Xn.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/3n2Xn.png" alt="enter image description here"></a></p>

CC BY-SA 4.0
2020-05-10T09:24:46.080Z
Thank you. now seems better but gives one statistics for all, what should I do to get one statistics in comparing each variable between two group?
Zizogolu
CC BY-SA 4.0
2020-05-10T09:43:59.613Z
You will need to play with the `comparisons` parameter within the `stat_compare_means()` function.
haci
CC BY-SA 4.0
2020-05-10T09:55:02.750Z
Sorry how? I have two groups pre and post which I placed them in comparison
Zizogolu
CC BY-SA 4.0
2020-05-10T10:02:19.940Z
A workaround would be doing `facet.by = "variable"` within your `ggboxplot()` call.
haci
CC BY-SA 4.0
2020-05-10T10:31:23.947Z
Thank you but gives no stat at all
Zizogolu
CC BY-SA 4.0
2020-05-10T13:06:05.410Z
Why do you call `stat_compare_means()` twice? And what happens when you use `stat_compare_means(aes(group = variable))`?
haci
CC BY-SA 4.0
2020-05-10T13:17:11.470Z
I removed extra stat_compare_means(), and stat_compare_means(aes(group = variable)) gives one stat
Zizogolu
CC BY-SA 4.0
2020-05-10T23:58:36.840Z
Thank you so much, are the stats are Kruskal-wallis?
Zizogolu
CC BY-SA 4.0
2020-05-10T07:30:40.367Z
2020-05-10T18:41:27.870Z
2020-05-10T18:41:27.870Z
haci
<p>I have a data frame for two groups and 9 variables </p>

<pre><code>&gt; head(aa)
  treatment variable     value
1       pre     Sig1 0.1173863
2       pre     Sig1 0.1888243
3       pre     Sig1 0.2191765
4       pre     Sig1 0.1277735
5       pre     Sig1 0.1711712
6       pre     Sig1 0.0000000
&gt; class(aa)
[1] "data.frame"
&gt; str(aa)
'data.frame':   1026 obs. of  3 variables:
 <span class="math-container">$ treatment: Factor w/ 2 levels "post","pre": 2 2 2 2 2 2 2 2 2 2 ...
 $</span> variable : Factor w/ 9 levels "Sig1","Sig2",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value    : num  0.117 0.189 0.219 0.128 0.171 ...
&gt; tail(aa)
     treatment variable value
1021      post     Sig9     0
1022      post     Sig9     0
1023      post     Sig9     0
1024      post     Sig9     0
1025      post     Sig9     0
1026      post     Sig9     0
&gt;
</code></pre>

<p>I am using this code</p>

<pre><code>library(ggpubr)
&gt; p &lt;- ggboxplot(aa, x = "variable", y = "value",color = "treatment")
&gt; my_comparisons &lt;- c("pre","post")
&gt; p + stat_compare_means(comparisons = my_comparisons)+ stat_compare_means(label.y = 50)  
Warning message:
Computation failed in `stat_signif()`:
missing value where TRUE/FALSE needed 
&gt; 
</code></pre>

<p>But I am getting this weird plot</p>

<p><a href="https://i.sstatic.net/72a87.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/72a87.png" alt="enter image description here"></a></p>

<p>While something like this is my goal</p>

<p><a href="https://i.sstatic.net/kI85K.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kI85K.png" alt="enter image description here"></a></p>

<p>I really don't know where I am doing wrong</p>

<p>I used ggplot where I got something like this but I need statistics that is why I have stuck on this </p>

<p><a href="https://i.sstatic.net/XM5WB.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/XM5WB.png" alt="enter image description here"></a></p>

<p>I then used this code </p>

<pre><code>&gt; DF &lt;- aa %&gt;% pivot_longer(.,cols = c(Sig1,Sig2,Sig3, Sig4,Sig5,Sig6,Sig7,Sig8,Sig9), names_to = "var", values_to = "val")
&gt; View(DF)
&gt; library(ggplot2)
&gt; library(ggpubr)
&gt; ggplot(DF, aes(x = treatment, y = val, fill = treatment))+
+     geom_boxplot()+
+     stat_compare_means(comparisons = c("pre","post")+
+     stat_compare_means()+
+     facet_grid(var~., scales = "free")
+ 
</code></pre>

<p>But I got another weird plot</p>

<p><a href="https://i.sstatic.net/pLvl1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/pLvl1.png" alt="enter image description here"></a></p>

<p>But changing the scale I got this but gives one statistics</p>

<p><a href="https://i.sstatic.net/mZMhI.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/mZMhI.png" alt="enter image description here"></a></p>

<p>While I need one stat for each variable between pre and post groups</p>

<p>This solution gives to statistics</p>

<pre><code>&gt; p &lt;- ggboxplot(aa, x = "variable", y = "value",color = "treatment",facet.by = "variable")
&gt; p + stat_compare_means(comparisons = my_comparisons)+ stat_compare_means(label.y =2)
</code></pre>

<p><a href="https://i.sstatic.net/2C08u.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/2C08u.png" alt="enter image description here"></a></p>

<p>When I edited my code</p>

<pre><code>&gt; p &lt;- ggboxplot(aa, x = "variable", y = "value",color = "treatment")
&gt; p + stat_compare_means(aes(group = variable))
&gt; 
</code></pre>

<p>But again one stat</p>

<p><a href="https://i.sstatic.net/hoxit.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/hoxit.png" alt="enter image description here"></a></p>

<ggplot2>
Getting weird plot with ggpubr package
# Getting weird plot with ggpubr package

(Asked by: username_0 on 2020-05-10)

I have a data frame for two groups and 9 variables 
> head(aa)
  treatment variable     value
1       pre     Sig1 0.1173863
2       pre     Sig1 0.1888243
3       pre     Sig1 0.2191765
4       pre     Sig1 0.1277735
5       pre     Sig1 0.1711712
6       pre     Sig1 0.0000000
> class(aa)
[1] "data.frame"
> str(aa)
'data.frame':   1026 obs. of  3 variables:
 $ treatment: Factor w/ 2 levels "post","pre": 2 2 2 2 2 2 2 2 2 2 ...
 $ variable : Factor w/ 9 levels "Sig1","Sig2",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value    : num  0.117 0.189 0.219 0.128 0.171 ...
> tail(aa)
     treatment variable value
1021      post     Sig9     0
1022      post     Sig9     0
1023      post     Sig9     0
1024      post     Sig9     0
1025      post     Sig9     0
1026      post     Sig9     0
>

I am using this code
library(ggpubr)
> p <- ggboxplot(aa, x = "variable", y = "value",color = "treatment")
> my_comparisons <- c("pre","post")
> p + stat_compare_means(comparisons = my_comparisons)+ stat_compare_means(label.y = 50)  
Warning message:
Computation failed in `stat_signif()`:
missing value where TRUE/FALSE needed 
> 

But I am getting this weird plot

While something like this is my goal

I really don't know where I am doing wrong
I used ggplot where I got something like this but I need statistics that is why I have stuck on this 

I then used this code 
> DF <- aa %>% pivot_longer(.,cols = c(Sig1,Sig2,Sig3, Sig4,Sig5,Sig6,Sig7,Sig8,Sig9), names_to = "var", values_to = "val")
> View(DF)
> library(ggplot2)
> library(ggpubr)
> ggplot(DF, aes(x = treatment, y = val, fill = treatment))+
+     geom_boxplot()+
+     stat_compare_means(comparisons = c("pre","post")+
+     stat_compare_means()+
+     facet_grid(var~., scales = "free")
+ 

But I got another weird plot

But changing the scale I got this but gives one statistics

While I need one stat for each variable between pre and post groups
This solution gives to statistics
> p <- ggboxplot(aa, x = "variable", y = "value",color = "treatment",facet.by = "variable")
> p + stat_compare_means(comparisons = my_comparisons)+ stat_compare_means(label.y =2)


When I edited my code
> p <- ggboxplot(aa, x = "variable", y = "value",color = "treatment")
> p + stat_compare_means(aes(group = variable))
> 

But again one stat

**Tags:** ggplot2

---

## 1 answers

### Answer by username_1 on 2020-05-10. Score: 3 | Accepted answer

The problem is the scale used: For the plot you called "weird" (first from the top), the scale is 50 and for the "ggplot only" (third from the top) the scale is 1.
You should play with the stat_compare_means(label.y = 50) bit, you can try setting the label.y parameter to 1.5 or 2.
EDIT BECAUSE OF THE ADDITIONAL REMARKS ON THE QUESTION:
Once again (and for sure the last time), I had to manually create example data as OP ignores repeated reminders on how to make a reproducible example with R.
library(data.table)
library(ggpubr)

text <- "
treatment variable     value
1       pre     Sig1 0.1173863
2       pre     Sig1 0.1888243
3       pre     Sig1 0.2191765
4       pre     Sig1 0.1277735
5       pre     Sig1 0.1711712
6       pre     Sig1 0.0000000
1       post     Sig1 0.1173863
2       post     Sig1 0.1888243
3       post     Sig1 0.2191765
4       post     Sig1 0.1277735
5       post     Sig1 0.1711712
6       post     Sig1 0.0000000
1021      pre     Sig9     0
1022      pre     Sig9     0
1023      pre     Sig9     0
1024      pre     Sig9     0
1025      pre     Sig9     0
1026      pre     Sig9     0
1021      post     Sig9     1
1022      post     Sig9     1
1023      post     Sig9     1
1024      post     Sig9     1
1025      post     Sig9     1
1026      post     Sig9     1
"
my_data <-fread(text)

> my_data
      V1 treatment variable     value
 1:    1       pre     Sig1 0.1173863
 2:    2       pre     Sig1 0.1888243
 3:    3       pre     Sig1 0.2191765
 4:    4       pre     Sig1 0.1277735
 5:    5       pre     Sig1 0.1711712
 6:    6       pre     Sig1 0.0000000
 7:    1      post     Sig1 0.1173863
 8:    2      post     Sig1 0.1888243
 9:    3      post     Sig1 0.2191765
10:    4      post     Sig1 0.1277735
11:    5      post     Sig1 0.1711712
12:    6      post     Sig1 0.0000000
13: 1021       pre     Sig9 0.0000000
14: 1022       pre     Sig9 0.0000000
15: 1023       pre     Sig9 0.0000000
16: 1024       pre     Sig9 0.0000000
17: 1025       pre     Sig9 0.0000000
18: 1026       pre     Sig9 0.0000000
19: 1021      post     Sig9 1.0000000
20: 1022      post     Sig9 1.0000000
21: 1023      post     Sig9 1.0000000
22: 1024      post     Sig9 1.0000000
23: 1025      post     Sig9 1.0000000
24: 1026      post     Sig9 1.0000000
      V1 treatment variable     value

ggboxplot(my_data,
          x = "treatment",
          y = "value",
          color = "treatment",
          facet.by = "variable") + 
  stat_compare_means(label = "p.format")

And this code generates:

> **Comments:**
> **username_0**: Thank you. now seems better but gives one statistics for all, what should I do to get one statistics in comparing each variable between two group?  
> **username_1**: You will need to play with the `comparisons` parameter within the `stat_compare_means()` function.  
> **username_0**: Sorry how? I have two groups pre and post which I placed them in comparison  
> **username_1**: A workaround would be doing `facet.by = "variable"` within your `ggboxplot()` call.  
> **username_0**: Thank you but gives no stat at all  
> **username_1**: Why do you call `stat_compare_means()` twice? And what happens when you use `stat_compare_means(aes(group = variable))`?  
> **username_0**: I removed extra stat_compare_means(), and stat_compare_means(aes(group = variable)) gives one stat  
> **username_0**: Thank you so much, are the stats are Kruskal-wallis?  

---

