Highlight data separately with facet_grid in R

I use facet_grid in R to build RT data for 5 different groups. I would like to highlight data between 5 and 95% for each group.

In the code below, I use the percentile of the entire data frame, not for each group. Any idea on how I can still use facet_grid and have a unique percentile for 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) 
+4
source share
1 answer

Thanks to the DWin suggestion, I used ave to find xmin and xmax for each group separately and included them directly in the chart command.

There may be a more elegant way to do this (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) 

enter image description here

0
source

Source: https://habr.com/ru/post/1434955/


All Articles