简体   繁体   中英

How to remove outlier based on manual threshold in ggplot geom_boxplot

I have the following script:

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

It produces plot like this:

在此处输入图像描述

As shown in the plot above. I'd like to remove outlier with hwy values <= 10.

In the data there are on rows where hwy <= 10 . For demonstration purposes let's remove rows where hwy <= 15 . You can remove the rows from the data of the 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

I would filter the data frame, like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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