简体   繁体   English

如何在ggplot2中使用face_grid绘制具有不同背景的箱线图?

[英]How to plot boxplots with different background using face_grid in ggplot2?

I have a series of boxplots (control vs patient) for different cases. 我有一系列针对不同情况的箱线图(对照vs病人)。 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. 我正在使用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. 我想使用pval $ sig绘制箱线图的背景。 For example, red for <0.3 and green for >0.3 . 例如,红色表示<0.3,绿色表示> 0.3。

I like messing with ggplot grobs, so here is one possibilty (I'm sure others can automate it better than this): 我喜欢把ggplot grobs弄乱了,所以这是一种可能性(我敢肯定其他人可以比这更好地实现自动化):

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: 我们要在面板上工作,因此是元素6、7、8。使用str可以找到我们需要更改的内容:

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)

结果图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM