首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用tidyverse和rstatix显示Tukey组?

如何使用tidyverse和rstatix显示Tukey组?
EN

Stack Overflow用户
提问于 2021-12-26 00:52:30
回答 2查看 313关注 0票数 1

在使用rstatix进行了Tukey测试之后,我想用Tukey组向数据帧中添加另一列。

代码语言:javascript
复制
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

我期待着这样的事情:

代码语言:javascript
复制
> 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

:如果可能的话,请帮我把组的字母放在方框的每一个盒子上。

EN

回答 2

Stack Overflow用户

发布于 2021-12-26 06:50:13

代码语言:javascript
复制
Tukey <- iris %>%
  tukey_hsd(Petal.Length ~ Species) %>%
  add_significance() %>% add_column(Group=c("A","B","C"))
票数 0
EN

Stack Overflow用户

发布于 2022-07-21 08:29:52

我无法使用tukey_hsd函数,但我认为这是一个使用其他包的可重置解决方案,希望它对您有用。)这个答案是基于这一个的。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70483632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档