【问题标题】:How to remove outlier based on manual threshold in ggplot geom_boxplot如何根据ggplot geom_boxplot中的手动阈值删除异常值
【发布时间】:2021-04-05 05:47:30
【问题描述】:

我有以下脚本:

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

它会产生这样的情节:

如上图所示。我想删除 hwy

【问题讨论】:

  • 为什么不在绘图前删除这些观察结果?
  • ggplot(filter(mpg, hwy >= 10), aes(class, hwy)) + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2) ?
  • @RonakShah 我想保持平均值和误差范围相同,只在最终图像中删除。我该怎么做?

标签: r ggplot2


【解决方案1】:

在数据中有hwy <= 10 的行。出于演示目的,让我们删除 hwy <= 15 所在的行。您可以从绘图的数据中删除行。

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

【讨论】:

    【解决方案2】:

    我会过滤数据框,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2015-07-16
      • 2023-02-18
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多