I have conducted my own statistics and have p.values which i want to add significance onto the bar graph stored as the variable 'txt'. I've followed the steps for manually adding annotations: https://const-ae.github.io/ggsignif/
But R is ignoring aes() within the geom_signif() shown below.
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)
This is what i am getting with no signficance being annotated on the bar graph.
Warning message:
In geom_signif(data = annotations_df, aes(xmin = start, xmax = end, :
Ignoring unknown aesthetics: xmin, xmax, annotations, and y_position
This is only a warning and actually the aesthetics don't get dropped, i.e. I think this is a "bug".
However, there are some issues with your code. First, when I run your code as is I get an error Treatment
not found which could be fixed by adding inherit.aes=FALSE
. Main issue however is that you fixed the limits of the y scale and as a consequence the annotations get dropped as with a y_position=.35
it falls outside of the limits. Fixing that too, your code works:
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))
)