'Computation failed in `stat_compare_means()`: Problem with `mutate()` input `p`. x all observations are in the same group

I'm trying to use ggpubr to show the kruskal-wallis p-value on my plot, but I get the error message:

Computation failed in `stat_compare_means()`:
Problem with `mutate()` input `p`.
x all observations are in the same group

Here's my code:

library(tidyverse)
library(nortest)
library(ggpubr)

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

df <- read.csv("30C static met opt_1.txt", header=TRUE, sep="\t")

my_comparisons <- list( c("465 + DMSO", "465 + Rad"), c("466 + DMSO", "466 + Rad"), c("1572", "1572 + 20 µM Met"), c("1572", "1572 + 30 µM Met"), c("1572", "1572 + 40 µM Met"), c("1572", "1572 + 50 µM Met"))

plot <- ggplot(data = df, aes(x = as.factor(Strain), y = as.numeric(OD), fill = Strain)) + 
  geom_boxplot(outlier.size = 0.5, fatten = 1) + 
  geom_dotplot(binaxis='y', stackdir='center', dotsize = 0.5) + 
  discrete_scale(aes(x = "Strain", y = "OD"), "OD", palette = cbPalette) +
  theme_light() +
  theme(panel.grid.major.x = element_blank()) + 
  labs(x = "Strain and drug") + 
  scale_y_continuous(limits = c(0.5, 2)) +
  scale_x_discrete(limits = c("465 + DMSO", "465 + Rad", "466 + DMSO", "466 + Rad", "1572", "1572 + 20 µM Met", "1572 + 30 µM Met", "1572 + 40 µM Met", "1572 + 50 µM Met")) +
  ylab(expression(paste(OD[595]))) +
  theme(legend.position = "none", axis.text.x = element_text(angle = 45, vjust = 0.5, size = 8)) +
  stat_compare_means(comparisons = my_comparisons, label = "p.adj", size = 3, step.increase = 0.15) +
  stat_compare_means()
plot

This gives me this plot and the above error message. boxplot

It's definitely the second stat_compare_means() that is causing the problem. But I haven't used a mutate() in my code. Is this ggpubr working behind the scenes?

my data looks like this:

    head(df)
      Strain     OD
1 465 + DMSO 1.3458
2 465 + DMSO 1.3514
3 465 + DMSO 1.3685
4 465 + DMSO 1.3661
5 465 + DMSO 1.3991
6 465 + DMSO 1.3922


Solution 1:[1]

I had the same issue and found out that the error only appears when I use scale_x_discrete() in the code. Without it, it runs just fine.

I fixed it by sorting the samples before plotting the graph:

df$Column_name <- factor(df$Column_name, levels = c("x", "y", "z"))

Maybe you could try:

my_sorted_list <- factor(c("465 + DMSO", "465 + Rad", "466 + DMSO", "466 + Rad", "1572", "1572 + 20 µM Met", "1572 + 30 µM Met", "1572 + 40 µM Met", "1572 + 50 µM Met")) 
scale_x_discrete(limits = my_sorted_list)

Solution 2:[2]

I am not sure if this helps, but I came here to solve an issue related to the error of doing this analysis across facet_grid() panels. I was using facet_grid(scales='free') and this same error was returning. Then after remove scales='free' from facet_grid(), it solved. It seems this functions has some issues dealing with multiple scales in the y or x scales

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Germano Neto