如何使用另一个测试结果中的标签来标记 ggplot 中的条形图?

Mar*_*rco 2 r ggplot2

我想用测试的标签输出来标记我的图,例如,使用 agricolae 库中的 LSD.test 进行 LSD 测试输出(a、b、ab 等)。这是运行的示例。

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"))
Run Code Online (Sandbox Code Playgroud)

通常,在其他库中,我测试数据,并将标签传递给文本图,但是可以在 ggplot 中做到这一点吗?例如,在 stat_summary() 中,使用 fun.y 中的 LSD.test ?

情节例如

And*_*rie 5

为此,您必须使用geom_text并指定其自己的数据集创建另一个标签图层。

lsd.test扩展包中的示例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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述