1

This is a bar plot of the iris dataset. The letters above each bar denote significant differences, obtained from a kruskal-wallis test. enter image description hereAlthough they are not too messy on this example plot, my real data has much larger error bars and differences in bar lengths. As such, the letters are all over the place and it becomes hard to read. I am wondering if it is possible to have the letters positioned beneath each bar, just above the x-axis. This way they would all align and be easy to read. What do ya reckon?

Code:

library(reshape2)
library(ggplot2)
library(agricolae)
library(Rmisc)

file<-iris

melt <- melt(file, id=c("Species"))

x1 <- summarySE(melt, measurevar = "value", groupvars = c("variable", "Species"), na.rm=TRUE)

d=list()
tmp=list()

for(i in 1:4){
  if(var(file[,i]) > 0){
    tmp<-c(tmp,colnames(file[i]))
    krusk <- kruskal(file[,i],file[,5],group=TRUE)
    krusk$groups<-krusk$groups[order(krusk$groups[,'trt']),]
    d[[i]]<-as.data.frame(krusk$groups)       
  }
} 

big_data=do.call(rbind,d)

plot<- ggplot(x1, aes(x = variable, y = value, fill = Species)) + 
  coord_flip()+
  geom_bar(stat = "identity", position =position_dodge(),colour="black",width=.7,size=.5)+ 
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.1,size=.5,position=position_dodge(.7))+
  theme(
    axis.text = element_text(angle=0, vjust=1,size=8,face="bold"),legend.title=element_blank(),legend.position="bottom",
    legend.text=element_text(face="italic"))+
  labs(title=NULL,x=NULL,y=NULL)+
  geom_text(aes(label=big_data$M,colour=Species),position=position_dodge(width=1),vjust=.8,hjust=-1,size=3)

plot
J.Con
  • 4,101
  • 4
  • 36
  • 64
  • Your code generates an error. To which package does `summaySE` function belong? But one problem could lie with the width parameters. The width parameters within `position-dodge()` differ. Make them the same. If you want the bars to butt up against each other, make the width parameter within `geom_bar()` the same as the width parameter within `position_dodge()`. Have a look at [how-to-position-the-text-on-bar-as-i-want](http://stackoverflow.com/questions/40211451/geom-text-how-to-position-the-text-on-bar-as-i-want/40229784#40229784) – Sandy Muspratt Nov 10 '16 at 03:35
  • Thanks for pointing that out. Edited post to include `library(Rmisc)`. I don't want the bars to butt up as that would make things even harder to read. Thanks for the link. I am able to move them around on the end of the bars, but I would like them at the start of the bars. :) – J.Con Nov 10 '16 at 03:41

1 Answers1

2

Something like this:
For geom_text, set y = 0, and hjust = 1.5.
Note that the dodging widths for bars, error bars, and text are the same. Also note the the width of the bars is equal to the dodging widths, and thus the bars butt up against each other within each variable.

plot <- ggplot(x1, aes(x = variable, y = value, fill = Species)) + 
  coord_flip() +
  geom_bar(stat = "identity", position = position_dodge(width = .7),
     colour = "black", width = .7, size = .5) + 
  geom_errorbar(aes(ymin = value-se, ymax = value+se), position=position_dodge(width = .7),
     width = .1, size = .5) +
  geom_text(aes(y = 0, label = big_data$M, colour = Species), 
     position=position_dodge(width = .7), hjust = 1.5, size = 3) +
  theme(
    axis.text = element_text(angle=0, vjust=1,size=8,face="bold"),legend.title=element_blank(),legend.position="bottom",
    legend.text=element_text(face="italic")) +
  labs(title=NULL,x=NULL,y=NULL)

plot
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122