gpt4 book ai didi

r - 是否有将 AOV 事后测试结果添加到 ggplot2 boxplot 的功能?

转载 作者:行者123 更新时间:2023-12-02 02:51:00 30 4
gpt4 key购买 nike

我想将 Tukey.HSD 事后测试的结果添加到 ggplot2 箱线图中。 This SO answer包含我想要的手动示例(即,绘图上的字母是手动添加的;共享字母的组是无法区分的,p>无论如何)。

enter image description here

是否有一个自动功能可以根据 AOV 和 Tukey HSD 事后分析将这些字母添加到箱线图中?

我认为编写这样的函数不会太难。它看起来像这样:

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))

我意识到 TukeyHSD 对象有一个 plot 方法,并且还有另一个包(我现在似乎找不到)可以完成我的任务在基本图形中进行描述,但我真的更喜欢在 ggplot2 中执行此操作。

最佳答案

您可以使用“multcompView”包中的“multcompLetters”在 Tukey HSD 测试后生成同源组的字母。从这里开始,需要提取与 Tukey HSD 中测试的每个因素相对应的组标签,以及箱线图中显示的上分位数,以便将标签放置在该水平之上。

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)
}

生成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))

Boxplot with automatic Tukey HSD group label placement

关于r - 是否有将 AOV 事后测试结果添加到 ggplot2 boxplot 的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18771516/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com