使用R中的facet_grid单独突出显示数据

Alb*_*lba 6 r facet ggplot2

我在R中使用facet_grid来绘制5个不同组的RT数据.我想强调每组5%到95%之间的数据.

使用下面的代码,我使用整个数据框的百分位数,而不是每个组的百分位数.任何想法我仍然可以使用facet_grid并在图中突出显示每个组的唯一百分位数.

rect <- data.frame (xmin=quantile(ss$RT, c(0.05)), 
                    xmax=quantile(ss$RT, c(0.95)), 
                    ymin=-Inf, ymax=Inf)


qplot(prevRT, RT, group=ss, color = prim, 
      geom = c("smooth"), 
      method="lm", data =ss) + 
   facet_grid(~ Groupe) + 
   geom_rect(data=rect, 
             aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), 
             color="grey20", alpha=0.5, inherit.aes = FALSE)
Run Code Online (Sandbox Code Playgroud)

Alb*_*lba 2

感谢 DWin 的建议,我使用ave分别找到每个组的 xmin 和 xmax,并将其直接合并到绘图命令中。

可能有一种更优雅的方法来做到这一点(欢迎提出建议),但它确实有效。

qplot(prevRT, RT, group=ss, color = prim, 
 geom = c("smooth"), 
 method="lm", data =ss) + 
 facet_grid(~ Groupe) + 
 geom_rect(data=ss, 
      aes(xmin=ave(ss$RT, ss$Groupe, FUN = function(x)quantile(x,c(0.05))),      
      xmax=ave(ss$RT, ss$Groupe, FUN = function(x)quantile(x,c(0.95))),
      ymin=-Inf,ymax=Inf), color="green", alpha=1/280, inherit.aes = FALSE)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述