I am trying to label both bars in comparison to a control and bars between conditions with asterixes to highlight significance.
When I am using a sidebyside bar plot the default setting for the position of the asterixes placed by geom_text is in the middle between the two conditions for each sample. I require this for the comparison between conditions (p2) but would like to place another set of asterixes from another level of significance (against control, p1) inside each bar.
I have tried hjust but this is relative to the graph size therefore everytime the graph size changes the astrix position also does.
Example code:
df = data.frame(names = rep(LETTERS[1:6], each=2),
num = c(rep(c("with","without"), 3)),
values = c(1,2,3,7,2,2,5,10,3,4,2,9),
p1=c(1,1,1,.005,0.5,1,0.005,0,1,0,1,1),
p2=c(1,1,1,1,1,1,1,0,1,1,1,0))
sidebyside=position_dodge(width=0.9)
df$star <- ""
df$star[df$p1 <= .05] <- "*"
df$star[df$p1 <= .01] <- "**"
df$star[df$p1 <= .001] <- "***"
df$star2 <- ""
df$star2[df$p2 <= .05] <- "*"
df$star2[df$p2 <= .01] <- "**"
df$star2[df$p2 <= .001] <- "***"
ggplot(df, aes(x=names, y=values, fill=num))+
geom_bar(stat="identity", position=sidebyside, colour='black')+
geom_text(aes(label=star), colour="black", vjust=1, size=10)+
geom_text(aes(label=star2), colour="black", vjust=0, size=10)
Thank you