0

Create some data

# Generate species 
species <- rep (c("Oak", "Elm", "Ash"), each = 10)

# Generate treatments
dose_1 <- rep (c("Ctrl"), 30)
dose_2 <- rep (c ("L"), 30)

# Generate results
result_1 <- c((runif(10, 9, 12)), runif(10, 14, 16), runif(10, 6, 8), (runif(10, 2, 5)), runif(10, 1, 4), runif(10, 2, 4))


# Combine into a sinlge dataframe
data <- data.frame (species = rep(species, 2), treatment = c(dose_1, dose_2), result = result_1)

Perform dunn_test using rstatix package

library(tidyverse)
library(rstatix)

data %>% 
  group_by(species) %>% 
  dunn_test(result ~ treatment, p.adjust.method = "holm")

Now how can I have group letter like the following

species treatment emmean    SE    df lower.CL upper.CL cld  
  <chr>   <fct>      <dbl> <dbl> <dbl>    <dbl>    <dbl> <chr>
1 Oak     Ctrl       10.6  0.249    18    10.1     11.1  A    
2 Oak     L           3.57 0.249    18     3.04     4.09 B    
3 Elm     Ctrl       15.2  0.252    18    14.7     15.8  A    
4 Elm     L           2.93 0.252    18     2.40     3.46 B    
5 Ash     Ctrl        7.15 0.173    18     6.79     7.51 A    
6 Ash     L           2.96 0.173    18     2.59     3.32 B

Here is one question where simple aov is applied. I want to utilize non-parametric test i.e. Dunn test.

UseR10085
  • 7,120
  • 3
  • 24
  • 54

1 Answers1

1

The function doesn't recognize the grouping you're trying to do with dplyr.

I wrote an R package that can do this automatically (with the Dunn test), but it currently uses the implementation from the pgirmess (not rstatix). There's also no way to do a holm adjustment yet, but you can do a Bonferroni correction by just dividing the threshold (p.value) by the number of comparisons (three in this case).

# installation
remotes::install_github("ethanbass/ggtukey")

library(ggplot2)
library(ggtukey)

ggplot(data, aes(x=treatment, y=result)) + 
geom_boxplot() + 
facet_wrap(~species) + 
ggtukey::geom_tukey(test = "kruskalmc", threshold = .05/3)

boxplot with Dunn letters

Edit: You can do a barplot like this:

ggplot(data, aes(x=treatment, y=result)) + 
  geom_bar(aes(fill=treatment), position="identity", 
           stat="summary", fun="mean") + 
  stat_summary(fun.data=mean_cl_normal, geom="errorbar", width=0.3) +
  facet_wrap(~species) + 
  ggtukey::geom_tukey(test = "kruskalmc", where = "cl_normal") 

bar plot with Dunn letters

Ethan Bass
  • 426
  • 3
  • 7
  • How can we add the group letters to barplot and also have a table view? Where is it specifying `Dunn test`? If you can integrate it with `rstatix` it will be great. – UseR10085 May 11 '23 at 04:58
  • I added the code to make a bar plot. There isn't currently an option to print the table unfortunately but that is certainly a good idea and would make a lot of sense. I will look into it. The Dunn test is specified with the `test` argument to `geom_tukey`. "kruskalmc" in this case refers to `pgirmess::kruskalmc`, which is a version of the Dunn test. I can also look into adding the rstatix Dunn test at some point, but I'm not sure when I'll have time. – Ethan Bass May 11 '23 at 13:34