为什么手动设置注释时 ggsignif aes() 参数被忽略?

问题描述 投票:0回答:1

我已经进行了自己的统计并获得了 p.values,我想将其显着性添加到存储为变量“txt”的条形图上。我已按照手动添加注释的步骤操作:https://const-ae.github.io/ggsignif/

但是 R 忽略了 geom_signif() 中的 aes(),如下所示。

df<- as.dataframe(
     Treatment=c("A","B"),
     mean=c(0.19,0.25),
     sem=c(0.01,0.02),
     n=c(195,233),
     max=c(0.21,0.17),
     min=c(0.17,0.24))

#txt = "*" from previous statistics

  if(txt!="n.s"){
    annotations_df<-data.frame(
      start = "A",
      end = "B",
      y= 0.35,
      label = txt
    )
  }

  plot<-df%>% #plots average only
 ggplot(df,aes(x=Treatment,y=mean,ymin=mean,ymax=max,fill=Treatment))+
    geom_col(color='black',size=0.7,width=0.8)+
    geom_errorbar(width=0.7,size=0.7,position = position_dodge(0.9))+
    scale_fill_manual(values=c("#028A0F","#B2D3C2"))+
    scale_y_continuous(limits=c(0,max(df$max)*1.5),expand = c(0,0))+#expand col touches x-axis
    theme_classic()+
    labs(y="Pellets consumed (#)")+
    theme(legend.position = "none",
          axis.text = element_text(size = 14, color = "black"),
          axis.title = element_text(size = 16, color = "black"))
  
  if(txt=="n.s"){plot}
  else {plot + geom_signif(
    data = annotations_df,
    aes(xmin=start,xmax=end,annotations=label,y_position=y),
    manual=TRUE)}
  
  print(plot)

这就是我得到的结果,在条形图上没有任何注释。

Warning message:
In geom_signif(data = annotations_df, aes(xmin = start, xmax = end,  :
  Ignoring unknown aesthetics: xmin, xmax, annotations, and y_position
r ggplot2 graph annotations
1个回答
0
投票

这只是一个警告,实际上美观性并没有下降,即我认为这是一个“错误”。

但是,您的代码存在一些问题。首先,当我按原样运行您的代码时,出现错误

Treatment
not found,可以通过添加
inherit.aes=FALSE
来修复。然而,主要问题是您修复了 y 比例的限制,因此注释被删除,就像
y_position=.35
超出了限制一样。也修复了这个问题,你的代码就可以工作了:

library(ggpubr)
library(ggplot2)

if (txt == "n.s") {
  plot
} else {
  plot <- plot +
    geom_signif(
      data = annotations_df,
      aes(xmin = start, xmax = end, annotations = label, y_position = y),
      manual = TRUE,
      inherit.aes = FALSE
    )
}

plot +
  scale_y_continuous(
    limits = c(0, NA),
    expand = expansion(mult = c(0, .05))
  )

© www.soinside.com 2019 - 2024. All rights reserved.