I have a series of boxplots (control vs patient) for different cases. I want to give different background color to the boxplot (not the fill in boxplot) depending upon the condition median(control)> median(patient). I am using facet_grid.
Edit:
library(data.table)
library(ggplot2)
set.seed(10)
my.t.test_p.value =function(...) {
obj=try(t.test(...), silent=TRUE)
if (is(obj, "try-error")) return(NA) else return(obj$p.value)
}
df <- data.frame(
site = sample(c("A", "B","C"), 30, replace = TRUE),
control = sample(c(0,1),30,replace = TRUE),
y=rnorm(30)
)
dt=data.table(df)
pval = dt[, list(pvalue = paste0("p= ",sprintf("%.3f", my.t.test_p.value(y~control)))), by=list(site)]
pval = as.data.frame(pval)
pval['sig']=as.numeric(gsub('p= ','',pval$pvalue)) <0.3 ## just for simplicity ;)
p=ggplot(data=df, aes(control,y)) + geom_boxplot(aes(fill=as.factor(control))) +
facet_grid(~site)
print(p)
I want to plot the backgrounds of boxplots using pval$sig. For example, red for <0.3 and green for >0.3 .