Extreme value labels in geom_boxplot

I am trying to add labels for extreme values ​​(outliers or not) on geom_box plots. I found this question that almost exactly resembles my [ extreme labels of ggplot2 values ​​in geom_boxplot ] The answer provided by yonicd almost works for me:

df=rbind(data.frame(id=rep("1",100),var=paste0("V",seq(1,100)),
         val=rnorm(100,0,5)),
         data.frame(id=rep("2",100),var=paste0("V",seq(1,100)),
         val=rnorm(100,0,3)))


df_bound=df%.%group_by(id)%>%do(.,data.frame(val=boxplot.stats(.$val)$out))
df_bound=left_join(df_bound, df, by=c("id","val"))

ggplot(df,aes(x=id, y=val, fill=id, label=var)) + geom_boxplot() +
geom_point(aes(group=id), data=df_bound)+
geom_text(aes(group=id), data=df_bound, hjust=-1, size=4)

It seems obvious that I just need to replace [$ out] in

 df_bound=df%.%group_by(id)%>%do(.,data.frame(val=boxplot.stats(.$val)$out))

to have extreme values ​​instead of outputs. If i use

df_bound=df%.%group_by(id)%>%do(.,data.frame(val=boxplot.stats(.$val)$stats))

labels for outputs are not displayed. How can this be fixed?

+4
source share
1 answer

, dplyr: %.% magrittr pipe %>%. [c (1,5)], "" "" boxplot.stats $.

df_bound <- df%>%group_by(id)%>%do(.,data.frame(val=boxplot.stats(.$val)$stats[c(1,5)]))

0

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


All Articles