是否有将AOV事后检测结果添加到ggplot2 boxplot的功能? [英] Is there a function to add AOV post-hoc testing results to ggplot2 boxplot?

查看:660
本文介绍了是否有将AOV事后检测结果添加到ggplot2 boxplot的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Tukey.HSD post-hoc测试的结果添加到 ggplot2 boxplot中。 这个SO答案包含我想要的手动示例(例如,剧情中的字母是手动添加的;分享的组一封信是无法区分的,无论如何)。





我有一个自动函数将这些字母添加到boxplot中,基于AOV和Tukey HSD事后分析吗?认为写这样一个函数不会太难。它看起来像这样:

pre $ set.seed(0)
lev < - gl(3,10 )
y <-c(rnorm(10),rnorm(10)+ 0.1,rnorm(10)+ 3)
d < - data.frame(lev = lev,y = y)

p_base< -ggplot(d,aes(x = lev,y = y))+ geom_boxplot()
$ b ba -aov(y_lev,data = d)
tHSD < - TukeyHSD(a)

#用于生成因子水平和相应标签的数据框的函数
generate_label_df < - 函数(HSD,factor_levels){
比较< - rownames(HSD $ l)
p.vals< - HSD $ l [,p adj]

##以某种方式创建一个字母向量
标签< - #使用比较和p.vals生成的字母向量,每个因子级别一个字母向量
letter_df< - data.frame(lev = factor_levels,labels = labels)
letter_df
}

#将标签添加到绘图
p_base +
geom_text(data = generate_label_df(tHSD),aes(x = 1,y = 0,label = labels))

我意识到 TukeyHSD 对象有一个 plot 方法,还有另外一个包(我现在无法找到)我用基本图形来描述,但我真的更喜欢在 ggplot2 中执行此操作。

解决方案

您可以使用'multcompView'包中的'multcompLetters'在Tukey HSD测试后生成同源组的字母。从那里,这是一个提取对应于Tukey HSD中测试的每个因素的组标签以及boxplot中显示的上分位数的问题,以便将标签放置在该级别之上。

  library(plyr)
library(ggplot2)
library(multcompView)

set.seed(0 )
lev < - gl(3,10)
y <-c(rnorm(10),rnorm(10)+ 0.1,rnorm(10)+ 3)
d - data.frame(lev = lev,y = y)

a -aov(y_lev,data = d)
tHSD < - TukeyHSD(a,ordered = FALSE,conf .level = 0.95)

generate_label_df< - 函数(HSD,flev){
#从Tukey事后
中提取标签和因子水平Tukey.levels < - HSD [[flev]] [,4]
Tukey.labels< - multcompLetters(Tukey.levels)['Letters']
plot.labels< - (Tukey.labels [['Letters' ]])

#为Tukey的5号码摘要获取最高分位数,并在
#上位分隔符和标签放映员之间添加一点空间缓冲区t
boxplot.df< - ddply(d,flev,function(x)max(fivenum(x $ y))+ 0.2)

#水平和Tukey的同质组字母
plot.levels< - data.frame(plot.labels,labels = Tukey.labels [['Letters']],
stringsAsFactors = FALSE)

#将它与标签合并
labels.df< - merge(plot.levels,boxplot.df,by.x ='plot.labels',by.y = flev,sort = FALSE)

return(labels.df)
}

生成ggplot

  p_base < -  ggplot(d,aes(x = lev,y = y))+ geom_boxplot()+ 
geom_text(data = generate_label_df(tHSD,'lev'),aes(x = plot.labels,y = V1,label = labels))


I'd like to add results of a Tukey.HSD post-hoc test to a ggplot2 boxplot. This SO answer contains a manual example of what I want (i.e., the letters on the plot were added manually; groups which share a letter are indistinguishable, p>whatever).

Is there an automatic function add letters like these to a boxplot, based on AOV and Tukey HSD post-hoc analyis?

I think it would not be too hard to write such a function. It would look something like this:

set.seed(0)
lev <- gl(3, 10)
y <- c(rnorm(10), rnorm(10) + 0.1, rnorm(10) + 3)
d <- data.frame(lev=lev, y=y)

p_base <- ggplot(d, aes(x=lev, y=y)) + geom_boxplot() 

a <- aov(y~lev, data=d)
tHSD <- TukeyHSD(a)

# Function to generate a data frame of factor levels and corresponding labels
generate_label_df <- function(HSD, factor_levels) {
  comparisons <- rownames(HSD$l)
  p.vals <- HSD$l[ , "p adj"]

  ## Somehow create a vector of letters
  labels <- # A vector of letters, one for each factor level, generated using `comparisons` and `p.vals`
  letter_df <- data.frame(lev=factor_levels, labels=labels)
  letter_df
}

# Add the labels to the plot
p_base + 
  geom_text(data=generate_label_df(tHSD), aes(x=l, y=0, label=labels))

I realize that the TukeyHSD object has a plot method, and there is another package (which I can't now seem to find) which does what I'm describing in base graphics, but I would really prefer to do this in ggplot2.

解决方案

You can use 'multcompLetters' from the 'multcompView' package to generate letters of homologous groups after a Tukey HSD test. From there, it's a matter of extracting the group labels corresponding to each factor tested in the Tukey HSD, as well as the upper quantile as displayed in the boxplot in order to place the label just above this level.

library(plyr)
library(ggplot2)
library(multcompView)

set.seed(0)
lev <- gl(3, 10)
y <- c(rnorm(10), rnorm(10) + 0.1, rnorm(10) + 3)
d <- data.frame(lev=lev, y=y)

a <- aov(y~lev, data=d)
tHSD <- TukeyHSD(a, ordered = FALSE, conf.level = 0.95)

generate_label_df <- function(HSD, flev){
 # Extract labels and factor levels from Tukey post-hoc 
 Tukey.levels <- HSD[[flev]][,4]
 Tukey.labels <- multcompLetters(Tukey.levels)['Letters']
 plot.labels <- names(Tukey.labels[['Letters']])

 # Get highest quantile for Tukey's 5 number summary and add a bit of space to buffer between    
 # upper quantile and label placement
    boxplot.df <- ddply(d, flev, function (x) max(fivenum(x$y)) + 0.2)

 # Create a data frame out of the factor levels and Tukey's homogenous group letters
  plot.levels <- data.frame(plot.labels, labels = Tukey.labels[['Letters']],
     stringsAsFactors = FALSE)

 # Merge it with the labels
   labels.df <- merge(plot.levels, boxplot.df, by.x = 'plot.labels', by.y = flev, sort = FALSE)

return(labels.df)
}

Generate ggplot

 p_base <- ggplot(d, aes(x=lev, y=y)) + geom_boxplot() +
  geom_text(data = generate_label_df(tHSD, 'lev'), aes(x = plot.labels, y = V1, label = labels))

这篇关于是否有将AOV事后检测结果添加到ggplot2 boxplot的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆