如何为Kruskal-Wallis的Dunn事后检验添加紧凑字母显示至表格与图表
Solution: Generate Compact Letter Display (CLD) for Kruskal-Wallis Dunn Test
Here's how to generate CLDs for your post-hoc results and integrate them into a summary table and ggplot:
Step 1: Load Required Packages
library(FSA) # For dunnTest library(dplyr) # For data manipulation library(multcompView) # For generating CLDs library(ggplot2) # For plotting
Step 2: Run Kruskal-Wallis and Dunn Post-Hoc Test
data <- iris # Kruskal-Wallis test kruskal <- kruskal.test(Petal.Width ~ Species, data = data) # Dunn post-hoc test with Bonferroni adjustment kruskal_ph <- dunnTest(Petal.Width ~ Species, data = data, method = "bonferroni")
Step 3: Extract Pairwise Comparisons and Generate CLD
First, reshape the Dunn test results into a format compatible with multcompView:
# Extract pairwise comparisons table comparisons <- kruskal_ph$res %>% select(Comparison, Z, P.adj) %>% separate(Comparison, into = c("group1", "group2"), sep = " - ") %>% mutate(Sig = case_when( P.adj > 0.05 ~ "ns", P.adj <= 0.01 ~ "**", P.adj <= 0.05 ~ "*" )) # Create a square p-value matrix for multcompView groups <- unique(c(comparisons$group1, comparisons$group2)) p_matrix <- matrix(1, nrow = length(groups), ncol = length(groups), dimnames = list(groups, groups)) # Fill matrix with adjusted p-values for (i in 1:nrow(comparisons)) { g1 <- comparisons$group1[i] g2 <- comparisons$group2[i] p_matrix[g1, g2] <- comparisons$P.adj[i] p_matrix[g2, g1] <- comparisons$P.adj[i] } # Generate CLD: groups with the same letter are not significantly different cld <- multcompLetters(p_matrix, threshold = 0.05)$Letters cld_df <- data.frame(Species = names(cld), CLD = unname(cld))
Step 4: Create Summary Table with CLD
Combine group-level statistics with the CLD letters:
summary_table <- data %>% group_by(Species) %>% summarise( Mean = round(mean(Petal.Width), 3), SD = round(sd(Petal.Width), 3), Median = round(median(Petal.Width), 3) ) %>% left_join(cld_df, by = "Species") # Add overall Kruskal-Wallis results as a footnote (optional) cat("Overall Kruskal-Wallis test: Chi-squared =", round(kruskal$statistic, 2), ", p-value =", round(kruskal$p.value, 4), "\n") print(summary_table)
Step 5: Add CLD to ggplot
Place the CLD letters above the mean bars:
# Prepare plot data with position for CLD letters plot_data <- summary_table %>% mutate(y_pos = Mean + SD + 0.1) # Adjust offset to avoid overlapping error bars # Generate plot ggplot(data, aes(x = Species, y = Petal.Width)) + geom_bar(stat = "summary", fun = "mean", aes(fill = Species), alpha = 0.7) + geom_errorbar(stat = "summary", fun.data = "mean_se", width = 0.2, color = "black") + geom_text(data = plot_data, aes(x = Species, y = y_pos, label = CLD), size = 5, fontface = "bold") + labs( title = "Petal Width by Species (Kruskal-Wallis Dunn Test)", x = "Species", y = "Petal Width (cm)" ) + theme_minimal() + theme(legend.position = "none")
Applying to Other Tests (e.g., Welch ANOVA pairwise.t.test)
The same workflow applies to other post-hoc tests:
- Run the test and extract pairwise p-values
- Reshape into a square p-value matrix
- Use
multcompLetters()to generate CLDs - Merge with summary stats and add to plots
Example for Welch ANOVA:
# Welch ANOVA welch <- oneway.test(Petal.Width ~ Species, data = data, var.equal = FALSE) # Pairwise t-test without pooling variance pairwise_welch <- pairwise.t.test(data$Petal.Width, data$Species, pool.sd = FALSE, p.adjust.method = "bonferroni") # Extract p-value matrix p_matrix_welch <- pairwise_welch$p.value # Generate CLD cld_welch <- multcompLetters(p_matrix_welch, threshold = 0.05)$Letters
内容的提问来源于stack exchange,提问作者MM1
相关产品推荐
相关产品推荐

