簡體   English   中英

如何根據ggplot geom_boxplot中的手動閾值去除異常值

[英]How to remove outlier based on manual threshold in ggplot geom_boxplot

我有以下腳本:

library(tidyverse)
ggplot(mpg, aes(class, hwy)) + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2)

它像這樣產生 plot :

在此處輸入圖像描述

如上圖plot所示。 我想刪除hwy值 <= 10 的異常值。

在數據中有hwy <= 10的行。 出於演示目的,讓我們刪除hwy <= 15的行。 您可以從 plot 的數據中刪除行。

plt<- ggplot(mpg, aes(class, hwy)) + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2)
plt$data <- subset(plt$data, hwy > 15)
plt

我會過濾數據框,如下所示:

ggplot(mpg %>% filter(hwy >= 10), aes(class, hwy)) + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM