1

我正在尝试使用 dplyr 为 geom_boxplot 添加标签以获取极值,并且与 ggplot 或 dplyr 不一致。我究竟做错了什么?

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

1 回答 1

2

这是解决问题的方法:

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)
于 2014-09-18T11:19:11.683 回答