簡體   English   中英

如何使用 tidyverse 和 rstatix 顯示 Tukey 組?

[英]How to show Tukey groups using tidyverse and rstatix?

使用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

額外:如果可能的話,請幫我把組的字母放在箱線圖的每個盒子上。

在此處輸入圖像描述

Tukey <- iris %>%
  tukey_hsd(Petal.Length ~ Species) %>%
  add_significance() %>% add_column(Group=c("A","B","C"))

我無法使用tukey_hsd function,但我認為這是使用其他軟件包的可重新設計的解決方案,希望它對您有用。 ;) 這個答案是基於這個的

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

上面的代碼應該產生箱線圖: 帶有 Tukey 字母的箱線圖!

PS.:我很高興我終於能夠在這里回答一些問題。 感謝stackoverflow的人:P

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM