stat_compare_means和多个组出现错误

时间:2019-05-09 15:49:39

标签: r ggplot2 ggpubr

我想用pvalue标记框图。

这是我的代码:

ggplot(df_annot,aes(x=Insect,y=index,fill=Fungi))+geom_boxplot(alpha=0.8)+
  geom_point(aes(fill=Fungi),size = 3, shape = 21,position = position_jitterdodge(jitter.width = 0.02,jitter.height = 0))+
  facet_wrap(~Location,scales="free" )+
  stat_compare_means(aes(group="Insect"))+
  guides(fill=guide_legend("M. robertii")) +
  scale_x_discrete(labels= c("I+","I-","soil alone"))+
  ylab(index_name)+
  theme(plot.title = element_text(size = 18, face = "bold"))+
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14)) + 
  theme(legend.text=element_text(size=14),
        legend.title=element_text(size=14)) +
  theme(strip.text.x = element_text(size = 14))

这是我收到的错误消息:

  

警告消息:
  1:未知或未初始化的列:“ p”。
  2:stat_compare_means()中的计算失败:参数“ x”丢失,没有默认值
  3:未知或未初始化的列:'p'。
  4:stat_compare_means()中的计算失败:参数“ x”丢失,没有默认值

我尝试从主ggplot调用移至aes()到boxplot调用。我在inherit.aes中尝试了不同的stat_compare_means()。 我尝试过先将根部分设置为子集,然后分别进行设置,但存在相同的错误。

感谢您的帮助。

谢谢

这是我的数据:

> dput(df_annot)
structure(list(Location = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Root", "Rhizospheric Soil"
), class = "factor"), Bean = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Bean", "No bean"), class = "factor"), 
Fungi = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L), .Label = c("M+", "M-"), class = "factor"), Insect = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Insect", 
"NI"), class = "factor"), index = c(2.90952191983974, 3.19997588762484, 
2.96753469534499, 2.93030877512644, 2.72220793003196, 3.09008037591454, 
2.63687890737919, 2.73583925812843, 3.06766793411045, 3.26431040286099, 
3.03361194852963, 2.9181623054061)), row.names = c("S-B1", 
"S-B2", "S-B3", "S-BF-1", "S-BF-2", "S-BF-3", "S-BFi-1", "S-BFi-2", 
"S-BFi-3", "S-Bi-1", "S-Bi-2", "S-Bi-3"), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

一种可能且容易修复的错误可能是使用确切的变量名(即,从变量名中删除双引号),而不是使用带引号的变量名(即字符)插入stat_compare_means(),因此该函数应看起来像像这样:

stat_compare_means(aes(group=Insect))

使用ggboxplot()的工作示例如下:

library(ggpubr)

boxplot <- ggboxplot(ToothGrowth, x = "dose", y = "len", add = "jitter",
                             color = "supp", group="supp", palette = "jco", legend.title="Supplier")
boxplot <- boxplot + stat_compare_means(aes(group=supp), label = "p.signif", method="wilcox.test", hide.ns=T, paired=F)

print(bxp.legend)

enter image description here 上面的示例有一条警告消息,但是我不知道如何改进代码以删除警告消息:

`cols` is now required.
Please use `cols = c(p)`