1

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 .

Roland
  • 127,288
  • 10
  • 191
  • 288
gruangly
  • 942
  • 8
  • 13
  • This could be an interesting question. However, you really should create a [small reproducible example](http://stackoverflow.com/a/5963610/1412059) to spare us the work. I'd give you an upvote if you do that and might even try to find an answer. – Roland Aug 22 '14 at 15:16
  • 1
    Maybe this is a starter: http://stackoverflow.com/questions/25260148/how-can-i-use-different-background-color-each-x-value-in-ggplot/25263495#25263495 – lukeA Aug 22 '14 at 15:26

1 Answers1

1

I like messing with ggplot grobs, so here is one possibilty (I'm sure others can automate it better than this):

library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot() + 
  facet_grid(.~gear) 

g <- ggplotGrob(p)

g
#TableGrob (7 x 9) "layout": 14 grobs
#    z     cells       name                                  grob
#1   0 (1-7,1-9) background       rect[plot.background.rect.1216]
#2   1 (3-3,4-4)  strip-top absoluteGrob[strip.absoluteGrob.1160]
#3   2 (3-3,6-6)  strip-top absoluteGrob[strip.absoluteGrob.1166]
#4   3 (3-3,8-8)  strip-top absoluteGrob[strip.absoluteGrob.1172]
#5   7 (4-4,3-3)     axis-l  absoluteGrob[GRID.absoluteGrob.1154]
#6   4 (4-4,4-4)      panel                gTree[GRID.gTree.1184]
#7   5 (4-4,6-6)      panel                gTree[GRID.gTree.1196]
#8   6 (4-4,8-8)      panel                gTree[GRID.gTree.1208]
#9   8 (5-5,4-4)     axis-b  absoluteGrob[GRID.absoluteGrob.1136]
#10  9 (5-5,6-6)     axis-b  absoluteGrob[GRID.absoluteGrob.1142]
#11 10 (5-5,8-8)     axis-b  absoluteGrob[GRID.absoluteGrob.1148]
#12 11 (6-6,4-8)       xlab          text[axis.title.x.text.1210]
#13 12 (4-4,2-2)       ylab          text[axis.title.y.text.1212]
#14 13 (2-2,4-8)      title            text[plot.title.text.1214]

We want to work on the panels, so that's elements 6, 7, 8. Using str we can find what we need to change:

str(g$grobs[[6]], 4)

#List of 5
# $ name         : chr "GRID.gTree.1184"
# $ gp           : NULL
# $ vp           : NULL
# $ children     :List of 3
#  ..$ grill.gTree.1183          :List of 5
#  .. ..$ name         : chr "grill.gTree.1183"
#  .. ..$ gp           : NULL
#  .. ..$ vp           : NULL
#  .. ..$ children     :List of 4
#  .. .. ..$ panel.background.rect.1176      :List of 10
#  .. .. .. ..- attr(*, "class")= chr [1:3] "rect" "grob" "gDesc"
#<snip>

str(g$grobs[[6]]$children[[1]]$children[[1]], 2)
#output omitted here 

g$grobs[[6]]$children[[1]]$children[[1]]$gp$fill <- "blue"
g$grobs[[7]]$children[[1]]$children[[1]]$gp$fill <- "red"
g$grobs[[8]]$children[[1]]$children[[1]]$gp$fill <- "green"

plot(g)

resulting plot

Roland
  • 127,288
  • 10
  • 191
  • 288