在R中使用geom_boxlot()+geom_jitter()时如何排除离群值 [英] How to exclude outliers when using geom_boxplot() + geom_jitter() in R
本文介绍了在R中使用geom_boxlot()+geom_jitter()时如何排除离群值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ggplot(data=mpg,mapping=aes(x=drv,y=cty))+geom_boxplot(outlier.shape = NA)+geom_jitter()
有没有办法从geom_jitter()中排除异常值?
推荐答案
geom_jitter()
本身没有丢弃离群值的理由。您需要通过定义哪些点为异常值来手动筛选要绘制的数据点。
library(dplyr)
library(ggplot2)
mpg %>%
group_by(drv) %>%
mutate(cty_filtered = case_when(cty - quantile(cty)[4] > 1.5*IQR(cty) ~ NA_real_,
quantile(cty)[2] - cty > 1.5*IQR(cty) ~ NA_real_,
TRUE ~ cty)) %>%
ggplot() + geom_boxplot(aes(drv, cty)) + geom_jitter(aes(drv, cty_filtered))
这篇关于在R中使用geom_boxlot()+geom_jitter()时如何排除离群值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文