簡體   English   中英

如何在geom_jitter中更改異常值點顏色

[英]How to change outliers points color in geom_jitter

如何更改geom_jitter異常值對應的點的參數(顏色、形狀等)?

有一個數據

> dput(head(df, 20))
structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L), .Label = c("W1", 
"W3", "W4", "W5", "W12", "W13", "W14"), class = "factor"), value = c(68, 
62, 174, 63, 72, 190, 73, 68, 62, 88, 81, 80, 79, 51, 73, 61, 
NA, NA, 84, 87)), row.names = c(NA, 20L), class = "data.frame")

和一個代碼

plot <-
    ggplot(df, aes(factor(df$variable), df$value)) +
    geom_jitter(position = position_jitter(width = .1, height = 0), size = 0.7) +
    theme(legend.position = 'none') +
    theme_classic() +
    labs(x = '',
         y = '',
         title = "test")

我得到了這樣的情節。

陰謀

對於之前創建的相同數據,默認coef = 1.5箱線圖,所以我知道這個數據集中存在異常值。 現在我只想創建點圖並將異常點塗成紅色。 隨着geom_boxplot ,這與單一功能的參數進行outlier.color ,但目前還沒有這樣的論據geom_jitter

您可以首先使用dplyr定義異常值:

library(dplyr)
new_df <- df %>% group_by(variable) %>% filter(!is.na(value)) %>% 
  mutate(Outlier = ifelse(value > quantile(value, 0.75)+1.50*IQR(value),"Outlier","OK")) %>%
  mutate(Outlier = ifelse(value < quantile(value, 0.25)-1.50*IQR(value),"Outlier",Outlier))

head(new_df)
# A tibble: 6 x 3
# Groups:   variable [1]
  variable value Outlier
  <fct>    <dbl> <chr>  
1 W1          68 OK     
2 W1          62 OK     
3 W1         174 Outlier
4 W1          63 OK     
5 W1          72 OK     
6 W1         190 Outlier

然后使用這個新列,您可以根據條件Outlier對數據集的子集進行大量處理:

library(ggplot2)
ggplot(subset(new_df, Outlier == "OK"), aes(x = variable, y = value))+
  geom_jitter(width = 0.1, size = 0.7)+
  geom_jitter(inherit.aes = FALSE, data = subset(new_df, Outlier == "Outlier"),
              aes(x = variable, y  = value), width = 0.1, size = 3, color = "red")+
  theme(legend.position = 'none') +
  theme_classic() +
  labs(x = '',
       y = '',
       title = "test")

在此處輸入圖片說明

暫無
暫無

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

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