extreme value labels ggplot2 in geom_boxplot

白昼怎懂夜的黑 提交于 2019-12-11 12:12:02

问题


I am trying to add labels to geom_boxplot for extreme values with dplyr and am getting an inconsistency either with ggplot or dplyr. what am i doing wrong?

#toy exmaple
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))) 

#subset with extreme values
df_bound=df%.%group_by(id)%.%filter(val<quantile(val,.025)|val>quantile(val,.975))

#plot 
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)

回答1:


this is a solution to the problem:

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)))

#new code
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)


来源:https://stackoverflow.com/questions/25910748/extreme-value-labels-ggplot2-in-geom-boxplot

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