在使用rstatix进行了Tukey测试之后,我想用Tukey组向数据帧中添加另一列。
library(tidyverse)
library(rstatix)
ggplot(data = iris,
aes(x = Species,
y = Petal.Length)) +
geom_boxplot()
Tukey <- iris %>%
tukey_hsd(Petal.Length ~ Species) %>%
add_significance() %>%
# ????? <------- Help here我期待着这样的事情:
> Tukey
# A tibble: 3 x 9
term group1 group2 null.value estimate conf.low conf.high p.adj p.adj.signif group
* <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 Species setosa versicolor 0 2.80 2.59 3.00 3e-15 **** A
2 Species setosa virginica 0 4.09 3.89 4.29 3e-15 **** B
3 Species versicolor virginica 0 1.29 1.09 1.50 3e-15 **** C:如果可能的话,请帮我把组的字母放在方框的每一个盒子上。

发布于 2021-12-26 06:50:13
Tukey <- iris %>%
tukey_hsd(Petal.Length ~ Species) %>%
add_significance() %>% add_column(Group=c("A","B","C"))发布于 2022-07-21 08:29:52
我无法使用tukey_hsd函数,但我认为这是一个使用其他包的可重置解决方案,希望它对您有用。)这个答案是基于这一个的。
library(multcompView)
# Calculate anova
iris_anova = aov(Petal.Length ~ Species, data = iris)
# Calculate tukey values, similar to tukey_hsd but not exactly the same!
iris_tukey = TukeyHSD(iris_anova)
# make the letter magic !!
cld = multcompLetters4(iris_anova, iris_tukey)
# table with factors and 3rd quantile
iris_tk = iris %>%
group_by(Species) %>%
summarise(mean=mean(Petal.Length, na.rm=T), quant = quantile(Petal.Length, probs = 0.75, na.rm=T)) %>%
arrange(desc(mean))
# extracting the compact letter display and adding to the Tk table
cld = as.data.frame.list(cld$Species)
iris_tk['cld'] = cld$Letters
# boxplot with the letters!!
ggplot(iris, aes(Species, Petal.Length)) +
geom_boxplot(aes(x=Species, Petal.Length ),show.legend = F)+
theme_bw() + #some theme fluff
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + #some more thematic fluff for cuteness
geom_text(data = iris_tk, aes(x = Species, y = quant, label = cld), size = 3, vjust=-1, hjust =-4) #that is the bit relevant here! note that vjust and hjust might need adjustments上面的代码应该会产生方框图:

我很高兴我终于能在这里回答一些事情。谢谢堆叠人员:P
https://stackoverflow.com/questions/70483632
复制相似问题