简体   繁体   English

geom_boxplot中的极值标签ggplot2

[英]extreme value labels ggplot2 in geom_boxplot

I am trying to add labels to geom_boxplot for extreme values with dplyr and am getting an inconsistency either with ggplot or dplyr. 我正在尝试使用dplyr将标签添加到geom_boxplot以获取极端值,并且使用ggplot或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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM