【问题标题】:adding ID to outliers in ggplot barplot in R将ID添加到R中ggplot barplot中的异常值
【发布时间】:2018-04-22 17:45:48
【问题描述】:

我创建了一个堆叠的条形图

ggplot(data %>% count(x, y),
        aes(x, n, fill = factor(y))) + 
geom_bar(stat="identity")+
theme_light()+
theme(plot.title = element_text(hjust=0.5))

在 50,54 和 60 处存在(可能的)异常值。如何将它们的 ID 添加到图表中?

【问题讨论】:

  • 你想如何确定一个值是异常值?高于设定阈值的任何值,超过 x 个平均值的标准偏差等。
  • 我基本上想将 ID 添加到 x 轴 50,54 和 60 处的值。我设法手动完成,但效率低下。该分布的平均值为 31.86 和 sd 6.07 - 因此阈值将超过 3 个标准差

标签: r ggplot2 bar-chart outliers


【解决方案1】:

如果您发布数据,我将使用它修改此答案。但基本上你想要

df %>%
    count(x, y) %>%
    ggplot(aes(x = x, y = n, fill = y)) +
    geom_col() +
    geom_text(aes(label = x), data = . %>% filter(x >= thresh), vjust = 0, nudge_y = 0.1)

其中thresh 是您设置的某个阈值——可能是一个有意义的任意截止点,或者可能与 x 的平均值相差 3 个标准差,或者其他什么。您可以将其存储在外部变量中,您可以在数据框中创建一个布尔列,或者您可以在 geom_text 内内联计算它——这完全取决于您。 vjust = 0, nudge_y = 0.1 将标签放在与异常值相对应的条形上方。

【讨论】:

    【解决方案2】:

    也许 geom_text(data=mydata%>%filter(just.the.outliers) ? 另请参阅:RE: Alignment of numbers on the individual bars with ggplot2

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多