简体   繁体   English

用R中的facet_grid分别突出显示数据

[英]Highlight data individually with facet_grid in R

I am using facet_grid in R to plot RT data for 5 different groups. 我在R中使用facet_grid来绘制5个不同组的RT数据。 I would like to highlight the data between 5 and 95% for each group. 我想强调每个组的5%到95%之间的数据。

With the code below, I am using the percentile of the entire data frame, not the one for each group. 在下面的代码中,我使用的是整个数据帧的百分位数,而不是每个组的百分位数。 Any idea of how I can still use facet_grid and have the unique percentile of each group highlighted in the plot. 关于如何仍然可以使用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)

Thanks to DWin's suggestion, I used ave to find xmin and xmax for each group individually and incorporated that directly into the command for the plot. 多亏了DWin的建议,我使用ave分别找到每个组的xmin和xmax并将其直接合并到绘图命令中。

There is probably a more elegant way to do that (and suggestions are welcome), but it works. 可能有一种更优雅的方法(欢迎提出建议),但是它可行。

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)

在此处输入图片说明

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

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