Q:

Is there a way to change the box plot color where data are significant in R

My data are as follows:

df1<-read.table(text = "time type
12  B88
19  B44
18  B44
13  B88
17  B44",header=TRUE)

I can use the following codes to get my plot:

ggplot(df1,aes(type,time)) + geom_boxplot(fill="green")+
  
    stat_compare_means(method = "t.test")

I want to get a different colour, let's say blue,  for the box which has a high medium when the P-value is less than 0.05%. Can we do it?
NOTE: I am not interested in running ttest

A:

ggplot(df1,aes(type,time)) + geom_boxplot(fill="green") +
stat_compare_means(method = "t.test") -> p #save your plot as p
build <- ggplot_build(p) # build plot
build$data[[1]][,"fill"] <- ifelse(build$data[[2]][1,"p.format"] < 0.05, list(c("blue","green")),list(rep("green",2))) # changes fill to blue if p value is < 0.05
plot(ggplot_gtable(build)) # plot new formatted graph

