How can I add Tukey TSF p value to plots?

17 views Asked by At

I am trying to do make Tukey HSD plot by adding p values as already answered in one of the previous questions, I am sharing the link to the questions already answered Link.

I am trying plot something similar and am using the same code that is provided here at the end to my dataset.

But when I implement the code I do not get the desired output.

I am sharing my sample dataset from my analysis

Here is my dataset

structure(list(Type = c("CONTROLE", "CONTROLE", "CONTROLE", "CONTROLE", 
"CONTROLE", "CONTROLE", "CONTROLE", "CONTROLE", "CONTROLE", "CONTROLE", 
"CONTROLE", "CONTROLE", "CONTROLE", "CONTROLE", "CONTROLE", "CONTROLE", 
"CONTROLE", "CONTROLE", "CONTROLE", "CONTROLE"), Roof = c("Banco", 
"Banco", "Banco", "Banco", "Banco", "Banco", "Banco", "Banco", 
"Banco", "Banco", "Banco", "Banco", "Banco", "Banco", "Banco", 
"Banco", "Banco", "Banco", "Banco", "Banco"), value = c("32,248", 
"32,56", "33,08", "31,033", "30,292", "31,035", "30,919", "29,762", 
"32,017", "29,76", "32,371", "30,799", "30,574", "29,982", "30,909", 
"31,183", "32,387", "30,82", "31,272", "30,326")), row.names = c(NA, 
20L), class = "data.frame")

df2<-data.table(df2)
df2$type<-as.factor(df2$type)
df2$roof<-as.factor(df2$roof)
df2$value<-as.numeric(df2$value)
dfl <- split(df2, df2$Roof, drop=TRUE)


doPlots <- function(x, signif.cutoff=.05){
    set.seed(123)
    p1 <- ggplot(dfl[[x]], aes(x=Type, y=value, fill=Type)) +
        geom_boxplot(alpha = 0.7, linetype = 1, size = 1.1) +
        theme(legend.position = "none") +
        scale_fill_brewer(palette = "Set2") +
        stat_boxplot(geom = "errorbar", size = 1.1, width = 0.4) +
        geom_jitter(aes(fill = Type), shape = 21, size = 4, stroke = 1) +
        ggtitle(x)
    a <- stats::TukeyHSD(stats::aov(value ~ Type, data = dfl[[x]]))[[1]]
    a <- stats::setNames(
        data.frame(
            do.call(rbind, strsplit(rownames(a), "-")),
            a[, "p adj"]
        ), c("Var1","var 2", "p") )
    a <- subset(a[stats::complete.cases(a),], p < signif.cutoff)
    if (nrow(a) == 0) return(p1) else {
        a$p <- formatC(signif(a$p, digits = 3),
                          digits = 3, format = "g", flag = "#")
        keep.tests <- unname(t(apply(a[, -3], 1, sort)))
        keep.tests <- unname(split(keep.tests, seq(dim(keep.tests)[1])))
    ts <- list(a = a, keep.tests = keep.tests)
    p1 + ggsignif::geom_signif(
        comparisons=ts$keep.tests,
        test="TukeyHSD",
        annotations=ts$a$p, 
        step_increase=0.1)
    }
}

plot_grid(plotlist=lapply(names(dfl), doPlots, signif.cutoff=.2), ncol=3)

after running the code I get the following error

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

I am attaching the output that i am actually expecting. In my case instead of analyste it would be type of roof (Banco or Toles) an dht eLabels would the the intervention type (control, soprema, sika, EPS).

Can someone help me figure out the error in my code?

0

There are 0 answers