2

I would like to label my plot with the label output of a test, eg., LSD test output (a, b, ab, etc) using LSD.test in library agricolae. Here is the running example.

library(ggplot2) 
library(agricolae)
wt<-gl(3,4,108,labels=c("W30","W60","W90")) 
pl<-gl(3,12,108,labels=c("P0","P1","P2")) 
gp<-gl(3,36,108,labels=c("A","B","C")) 

dat<-cbind(
  A=runif(108),
  B=runif(108,min=1,max=10),
  C=runif(108,min=100,max=200),
  D=runif(108,min=1000,max=1500)
) 
dat.df<-data.frame(wt,pl,gp,dat) 
dat.m<-melt(dat.df) 

ggplot(dat.m,aes(x=wt,y=value,group=pl,facet=gp,fill=pl))+         
  stat_summary(fun.y=mean,geom="bar",size=2,position="dodge")+         
  stat_summary(fun.ymin=function(x)(mean(x)-sd(x)/sqrt(length(x))),geom="errorbar", 
  fun.ymax=function(x)(mean(x)+sd(x)/sqrt(length(x))),position="dodge")+
  facet_grid(variable~facet,scale="free_y")+ 
  opts(legend.position="top")+                      
  scale_colour_manual(values = c("red", "blue", "green"))

Normally, in other library, I tested the data, and pass the label to the text plot, but is it possible to do it in ggplot? eg., in stat_summary(), that use the LSD.test within fun.y?

The plot for example

Marco
  • 505
  • 9
  • 18
  • The running example for the plot is OK. But could you give us some example code that actually produces the labels? No clue which ones you want to compare... – Joris Meys May 11 '11 at 12:35
  • To do what you want I usually summarize the data outside ggplot (with plyr for example) then I add the letters from the test to the data.frame and plot the letters with geom_text. – Luciano Selzer May 11 '11 at 13:15

2 Answers2

5

To achieve this, you have to create another label layer using geom_text and specify its own dataset.

Expanding on the example in lsd.test in package agricolae:

library(agricolae)
library(ggplot2)

data(sweetpotato)
model <- aov(yield~virus, data=sweetpotato)
lsd <- LSD.test(model,"virus",p.adj="bon")

ggplot() + 
  stat_summary(data=sweetpotato, aes(x=virus, y=yield), fun.y=mean, geom="bar") +
  geom_text(data=lsd, aes(x=trt, y=means, label=round(means, 1)), vjust=0)

enter image description here

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • Thanks very much, @Andrie, you did helped me a lot! I tried to use your idea on my example code, but did not succeed. As I want to test the data in each row (i.e, the variable in my example), it seems that I need to do the test, using multiple factors, and then split the 'trt' into seperate factors, and include them into the aes() dataframe. that seems not so straight forward, what is your opinion? And, I also would like to know, is there any way to lable some individual bars, using, for example, the asterisk (*), in one plot? – Marco May 12 '11 at 00:13
0

you can try this

first, defined a vector based on the pvalue, like this:

padj=ifelse(pval<0.001,'*','')

then, add padj column to your data.fram,

 head(df)
                                name               type number padj
1                  metabolic process biological_process    968    *
2                 catalytic activity molecular_function    801    *
3         cellular metabolic process biological_process    617    *
4               biosynthetic process biological_process    357    *
5 cellular protein metabolic process biological_process    279     
6                          cytoplasm cellular_component    202    *

and finally, add geom_text to your plot

p <- ggplot(data=df, aes(x=name,y=number,fill=type))+
geom_bar(position=position_dodge())+
scale_fill_brewer(palette="Set2")+
geom_text(aes(label=padj), vjust=0.25,hjust=0.25)
p