If it is fine for you to switch to another package capable of performing the dunnTest function, then the FSA
package is one solution. Then, using the rcompanion
package you can get the compact letter display of your comparisons. As I do not have access to your MR
data, I used the mtcars
data from R
to show how to do it in this way:
# install and load the required packages
library(FSA)
library(rcompanion)
# using mtcars as example data and a new assignment as df
df <- mtcars
# using cyl as factor variable and hp as dependent variable
df$cyl <- factor(df$cyl,levels = c("4","6","8"))
# running kruskal-wallis test if there is any difference between groups
kruskal.test(hp ~ cyl,
data = df)
Kruskal-Wallis rank sum test
data: hp by cyl
Kruskal-Wallis chi-squared = 25.222, df = 2, p-value = 3.336e-06
# running post hoc Dunn test
Phocdunn <- dunnTest(hp ~ cyl,
data=df,
method="bonferroni") # Can adjust p-values;
Phocdunn
Comparison Z P.unadj P.adj
1 4 - 6 -1.823778 6.818569e-02 2.045571e-01
2 4 - 8 -4.989580 6.051057e-07 1.815317e-06
3 6 - 8 -2.437999 1.476883e-02 4.430648e-02
# extract the post hoc from Phocdunn
Phocdunns <- Phocdunn$res
# Compact letter display of comparisons
cld <- cldList(comparison = Phocdunns$Comparison,
p.value = Phocdunns$P.adj,
threshold = 0.05)[1:2]
names(cld)[1]<-"cyl" # change the name of grouping factor according to the dataset (df)
cld
# here it shows the cyl levels 4 and 6 are not different as they have same letter #but different from cyl 8
cyl Letter
1 4 a
2 6 a
3 8 b
# visualization of comparisons using ggplot (you need to adjust the plot arguments based on your dataset)
library(tidyverse)
ggplot(data=df, aes(x=cyl, y=hp,col=cyl))+
scale_color_brewer(palette = "Dark2")+
geom_jitter(position = position_jitterdodge(jitter.width = 1), size = 0.5)+
theme_bw() +
geom_text(data = cld, aes(label = cld$Letter, y = 300, x = cyl),
vjust = -0.5,
hjust= 0.5,
fontface = "bold",
size=3.5,
check_overlap = F)+
theme(legend.position = "none")
