ggplot 中的自动异常值标记

Sku*_*rup 2 r ggplot2 ggrepel

我在循环中使用 ggplot 为 200 个变量(V1、V2 等)中的每一个生成散点图。为了使散点图更清晰,我希望能够自动标记异常值。我想为每个唯一变量标记大于第 95 个百分位值的点。

我尝试使用此处的代码 - Label points in geom_point,但是,这更多的是标记异常值的手动方法。我有大约 200 个变量,无法指定每个变量的值。

同样,我能找到的最接近的解决方案来自上面的链接:county_list[i] 是我循环的变量列表

    ggplot(nba, aes(x= county_list[i], y= Afd_2017, colour="green", label=Name))+
    geom_point() +
    geom_text(aes(label=ifelse(value_of_V[i]>24,as.character(Name),'')),hjust=0,vjust=0)
Run Code Online (Sandbox Code Playgroud)

我想要的是这样的:

    ggplot(nba, aes(x= county_list[i], y= Afd_2017, colour="green", label=Name))+
    geom_point() +
    geom_text(aes(label=ifelse((value_of_V[i] >greater-than- 
    value-of-the-95-Percentile-of-the- 
    value_of_V[i]),as.character(Name),'')),hjust=0,vjust=0)
Run Code Online (Sandbox Code Playgroud)

Ron*_*hah 6

lapply您可以使用/创建绘图列表map

library(ggplot2)

list_plots <- lapply(nba[-1], function(data) 
     ggplot(nba, aes(x= MIN, y = data, colour="green", label=Name))+
     geom_point() +
     geom_text(aes(label= ifelse(data > quantile(data, 0.95),
     as.character(Name),'')),hjust=0,vjust=0))
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过使用以下命令对列表进行子集化来访问各个图[[

list_plots[[6]]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

list_plots[[7]]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述