在R中使用facet_grid分别突出显示数据 [英] Highlight data individually with facet_grid in R

查看:66
本文介绍了在R中使用facet_grid分别突出显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using facet_grid in R to plot RT data for 5 different groups. I would like to highlight the data between 5 and 95% for each group.

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

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.

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)

推荐答案

由于DWin的建议,我使用 ave 分别查找每个组的xmin和xmax,并将其直接合并到阴谋.

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.

可能有一种更优雅的方法(欢迎提出建议),但它可以工作.

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)

这篇关于在R中使用facet_grid分别突出显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆