Highlight data individually with facet_grid in R

☆樱花仙子☆ 提交于 2020-01-21 11:13:28

问题


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.

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)

回答1:


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)



来源:https://stackoverflow.com/questions/12485979/highlight-data-individually-with-facet-grid-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!